// Copyright 2017 The Gitea Authors. All rights reserved. // Use of this source code is governed by a MIT-style // license that can be found in the LICENSE file. package models import ( "errors" "code.gitea.io/gitea/modules/auth/openid" "code.gitea.io/gitea/modules/log" ) // ErrOpenIDNotExist openid is not known var ErrOpenIDNotExist = errors.New("OpenID is unknown") // UserOpenID is the list of all OpenID identities of a user. type UserOpenID struct { ID int64 `xorm:"pk autoincr"` UID int64 `xorm:"INDEX NOT NULL"` URI string `xorm:"UNIQUE NOT NULL"` Show bool `xorm:"DEFAULT false"` } // GetUserOpenIDs returns all openid addresses that belongs to given user. func GetUserOpenIDs(uid int64) ([]*UserOpenID, error) { openids := make([]*UserOpenID, 0, 5) if err := x. Where("uid=?", uid). Asc("id"). Find(&openids); err != nil { return nil, err } return openids, nil } // isOpenIDUsed returns true if the openid has been used. func isOpenIDUsed(e Engine, uri string) (bool, error) { if len(uri) == 0 { return true, nil } return e.Get(&UserOpenID{URI: uri}) } // NOTE: make sure openid.URI is normalized already func addUserOpenID(e Engine, openid *UserOpenID) error { used, err := isOpenIDUsed(e, openid.URI) if err != nil { return err } else if used { return ErrOpenIDAlreadyUsed{openid.URI} } _, err = e.Insert(openid) return err } // AddUserOpenID adds an pre-verified/normalized OpenID URI to given user. func AddUserOpenID(openid *UserOpenID) error { return addUserOpenID(x, openid) } // DeleteUserOpenID deletes an openid address of given user. func DeleteUserOpenID(openid *UserOpenID) (err error) { var deleted int64 // ask to check UID address := UserOpenID{ UID: openid.UID, } if openid.ID > 0 { deleted, err = x.ID(openid.ID).Delete(&address) } else { deleted, err = x. Where("openid=?", openid.URI). Delete(&address) } if err != nil { return err } else if deleted != 1 { return ErrOpenIDNotExist } return nil } // ToggleUserOpenIDVisibility toggles visibility of an openid address of given user. func ToggleUserOpenIDVisibility(id int64) (err error) { _, err = x.Exec("update `user_open_id` set `show` = not `show` where `id` = ?", id) return err } // GetUserByOpenID returns the user object by given OpenID if exists. func GetUserByOpenID(uri string) (*User, error) { if len(uri) == 0 { return nil, ErrUserNotExist{0, uri, 0} } uri, err := openid.Normalize(uri) if err != nil { return nil, err } log.Trace("Normalized OpenID URI: " + uri) // Otherwise, check in openid table oid := &UserOpenID{} has, err := x.Where("uri=?", uri).Get(oid) if err != nil { return nil, err } if has { return GetUserByID(oid.UID) } return nil, ErrUserNotExist{0, uri, 0} } nput type='hidden' name='h' value='v1.20.0-rc2'/>
path: root/main.go
blob: 30dbf2766224c99ccf6195cde25aae10132e8531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192