@@ -24,6 +24,7 @@ import (
24
24
"code.gitea.io/gitea/modules/log"
25
25
)
26
26
27
+ // LoginType represents an login type.
27
28
type LoginType int
28
29
29
30
// Note: new type must append to the end of list to maintain compatibility.
@@ -36,13 +37,15 @@ const (
36
37
LoginDLDAP // 5
37
38
)
38
39
40
+ // LoginNames contains the name of LoginType values.
39
41
var LoginNames = map [LoginType ]string {
40
42
LoginLDAP : "LDAP (via BindDN)" ,
41
43
LoginDLDAP : "LDAP (simple auth)" , // Via direct bind
42
44
LoginSMTP : "SMTP" ,
43
45
LoginPAM : "PAM" ,
44
46
}
45
47
48
+ // SecurityProtocolNames contains the name of SecurityProtocol values.
46
49
var SecurityProtocolNames = map [ldap.SecurityProtocol ]string {
47
50
ldap .SecurityProtocolUnencrypted : "Unencrypted" ,
48
51
ldap .SecurityProtocolLDAPS : "LDAPS" ,
@@ -56,22 +59,28 @@ var (
56
59
_ core.Conversion = & PAMConfig {}
57
60
)
58
61
62
+ // LDAPConfig holds configuration for LDAP login source.
59
63
type LDAPConfig struct {
60
64
* ldap.Source
61
65
}
62
66
67
+ // FromDB fills up a LDAPConfig from serialized format.
63
68
func (cfg * LDAPConfig ) FromDB (bs []byte ) error {
64
69
return json .Unmarshal (bs , & cfg )
65
70
}
66
71
72
+ // ToDB exports a LDAPConfig to a serialized format.
67
73
func (cfg * LDAPConfig ) ToDB () ([]byte , error ) {
68
74
return json .Marshal (cfg )
69
75
}
70
76
77
+ // SecurityProtocolName returns the name of configured security
78
+ // protocol.
71
79
func (cfg * LDAPConfig ) SecurityProtocolName () string {
72
80
return SecurityProtocolNames [cfg .SecurityProtocol ]
73
81
}
74
82
83
+ // SMTPConfig holds configuration for the SMTP login source.
75
84
type SMTPConfig struct {
76
85
Auth string
77
86
Host string
@@ -81,22 +90,27 @@ type SMTPConfig struct {
81
90
SkipVerify bool
82
91
}
83
92
93
+ // FromDB fills up an SMTPConfig from serialized format.
84
94
func (cfg * SMTPConfig ) FromDB (bs []byte ) error {
85
95
return json .Unmarshal (bs , cfg )
86
96
}
87
97
98
+ // ToDB exports an SMTPConfig to a serialized format.
88
99
func (cfg * SMTPConfig ) ToDB () ([]byte , error ) {
89
100
return json .Marshal (cfg )
90
101
}
91
102
103
+ // PAMConfig holds configuration for the PAM login source.
92
104
type PAMConfig struct {
93
105
ServiceName string // pam service (e.g. system-auth)
94
106
}
95
107
108
+ // FromDB fills up a PAMConfig from serialized format.
96
109
func (cfg * PAMConfig ) FromDB (bs []byte ) error {
97
110
return json .Unmarshal (bs , & cfg )
98
111
}
99
112
113
+ // ToDB exports a PAMConfig to a serialized format.
100
114
func (cfg * PAMConfig ) ToDB () ([]byte , error ) {
101
115
return json .Marshal (cfg )
102
116
}
@@ -115,13 +129,15 @@ type LoginSource struct {
115
129
UpdatedUnix int64
116
130
}
117
131
118
- func (s * LoginSource ) BeforeInsert () {
119
- s .CreatedUnix = time .Now ().Unix ()
120
- s .UpdatedUnix = s .CreatedUnix
132
+ // BeforeInsert is invoked from XORM before inserting an object of this type.
133
+ func (source * LoginSource ) BeforeInsert () {
134
+ source .CreatedUnix = time .Now ().Unix ()
135
+ source .UpdatedUnix = source .CreatedUnix
121
136
}
122
137
123
- func (s * LoginSource ) BeforeUpdate () {
124
- s .UpdatedUnix = time .Now ().Unix ()
138
+ // BeforeUpdate is invoked from XORM before updating this object.
139
+ func (source * LoginSource ) BeforeUpdate () {
140
+ source .UpdatedUnix = time .Now ().Unix ()
125
141
}
126
142
127
143
// Cell2Int64 converts a xorm.Cell type to int64,
@@ -135,6 +151,7 @@ func Cell2Int64(val xorm.Cell) int64 {
135
151
return (* val ).(int64 )
136
152
}
137
153
154
+ // BeforeSet is invoked from XORM before setting the value of a field of this object.
138
155
func (source * LoginSource ) BeforeSet (colName string , val xorm.Cell ) {
139
156
switch colName {
140
157
case "type" :
@@ -151,41 +168,49 @@ func (source *LoginSource) BeforeSet(colName string, val xorm.Cell) {
151
168
}
152
169
}
153
170
154
- func (s * LoginSource ) AfterSet (colName string , _ xorm.Cell ) {
171
+ // AfterSet is invoked from XORM after setting the value of a field of this object.
172
+ func (source * LoginSource ) AfterSet (colName string , _ xorm.Cell ) {
155
173
switch colName {
156
174
case "created_unix" :
157
- s .Created = time .Unix (s .CreatedUnix , 0 ).Local ()
175
+ source .Created = time .Unix (source .CreatedUnix , 0 ).Local ()
158
176
case "updated_unix" :
159
- s .Updated = time .Unix (s .UpdatedUnix , 0 ).Local ()
177
+ source .Updated = time .Unix (source .UpdatedUnix , 0 ).Local ()
160
178
}
161
179
}
162
180
181
+ // TypeName return name of this login source type.
163
182
func (source * LoginSource ) TypeName () string {
164
183
return LoginNames [source .Type ]
165
184
}
166
185
186
+ // IsLDAP returns true of this source is of the LDAP type.
167
187
func (source * LoginSource ) IsLDAP () bool {
168
188
return source .Type == LoginLDAP
169
189
}
170
190
191
+ // IsDLDAP returns true of this source is of the DLDAP type.
171
192
func (source * LoginSource ) IsDLDAP () bool {
172
193
return source .Type == LoginDLDAP
173
194
}
174
195
196
+ // IsSMTP returns true of this source is of the SMTP type.
175
197
func (source * LoginSource ) IsSMTP () bool {
176
198
return source .Type == LoginSMTP
177
199
}
178
200
201
+ // IsPAM returns true of this source is of the PAM type.
179
202
func (source * LoginSource ) IsPAM () bool {
180
203
return source .Type == LoginPAM
181
204
}
182
205
206
+ // HasTLS returns true of this source supports TLS.
183
207
func (source * LoginSource ) HasTLS () bool {
184
208
return ((source .IsLDAP () || source .IsDLDAP ()) &&
185
209
source .LDAP ().SecurityProtocol > ldap .SecurityProtocolUnencrypted ) ||
186
210
source .IsSMTP ()
187
211
}
188
212
213
+ // UseTLS returns true of this source is configured to use TLS.
189
214
func (source * LoginSource ) UseTLS () bool {
190
215
switch source .Type {
191
216
case LoginLDAP , LoginDLDAP :
@@ -197,6 +222,8 @@ func (source *LoginSource) UseTLS() bool {
197
222
return false
198
223
}
199
224
225
+ // SkipVerify returns true if this source is configured to skip SSL
226
+ // verification.
200
227
func (source * LoginSource ) SkipVerify () bool {
201
228
switch source .Type {
202
229
case LoginLDAP , LoginDLDAP :
@@ -208,17 +235,23 @@ func (source *LoginSource) SkipVerify() bool {
208
235
return false
209
236
}
210
237
238
+ // LDAP returns LDAPConfig for this source, if of LDAP type.
211
239
func (source * LoginSource ) LDAP () * LDAPConfig {
212
240
return source .Cfg .(* LDAPConfig )
213
241
}
214
242
243
+ // SMTP returns SMTPConfig for this source, if of SMTP type.
215
244
func (source * LoginSource ) SMTP () * SMTPConfig {
216
245
return source .Cfg .(* SMTPConfig )
217
246
}
218
247
248
+ // PAM returns PAMConfig for this source, if of PAM type.
219
249
func (source * LoginSource ) PAM () * PAMConfig {
220
250
return source .Cfg .(* PAMConfig )
221
251
}
252
+
253
+ // CreateLoginSource inserts a LoginSource in the DB if not already
254
+ // existing with the given name.
222
255
func CreateLoginSource (source * LoginSource ) error {
223
256
has , err := x .Get (& LoginSource {Name : source .Name })
224
257
if err != nil {
@@ -231,6 +264,7 @@ func CreateLoginSource(source *LoginSource) error {
231
264
return err
232
265
}
233
266
267
+ // LoginSources returns a slice of all login sources found in DB.
234
268
func LoginSources () ([]* LoginSource , error ) {
235
269
auths := make ([]* LoginSource , 0 , 5 )
236
270
return auths , x .Find (& auths )
@@ -248,11 +282,13 @@ func GetLoginSourceByID(id int64) (*LoginSource, error) {
248
282
return source , nil
249
283
}
250
284
285
+ // UpdateSource updates a LoginSource record in DB.
251
286
func UpdateSource (source * LoginSource ) error {
252
287
_ , err := x .Id (source .ID ).AllCols ().Update (source )
253
288
return err
254
289
}
255
290
291
+ // DeleteSource deletes a LoginSource record in DB.
256
292
func DeleteSource (source * LoginSource ) error {
257
293
count , err := x .Count (& User {LoginSource : source .ID })
258
294
if err != nil {
@@ -357,13 +393,16 @@ func (auth *smtpLoginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
357
393
return nil , nil
358
394
}
359
395
396
+ // SMTP authentication type names.
360
397
const (
361
398
SMTPPlain = "PLAIN"
362
399
SMTPLogin = "LOGIN"
363
400
)
364
401
402
+ // SMTPAuths contains available SMTP authentication type names.
365
403
var SMTPAuths = []string {SMTPPlain , SMTPLogin }
366
404
405
+ // SMTPAuth performs an SMTP authentication.
367
406
func SMTPAuth (a smtp.Auth , cfg * SMTPConfig ) error {
368
407
c , err := smtp .Dial (fmt .Sprintf ("%s:%d" , cfg .Host , cfg .Port ))
369
408
if err != nil {
@@ -487,6 +526,7 @@ func LoginViaPAM(user *User, login, password string, sourceID int64, cfg *PAMCon
487
526
return user , CreateUser (user )
488
527
}
489
528
529
+ // ExternalUserLogin attempts a login using external source types.
490
530
func ExternalUserLogin (user * User , login , password string , source * LoginSource , autoRegister bool ) (* User , error ) {
491
531
if ! source .IsActived {
492
532
return nil , ErrLoginSourceNotActived
0 commit comments