Skip to content

Commit ab9bb54

Browse files
Add microsoft oauth2 providers (#16544)
* Clean up oauth2 providers Signed-off-by: Andrew Thornton <art27@cantab.net> * Add AzureAD, AzureADv2, MicrosoftOnline OAuth2 providers Signed-off-by: Andrew Thornton <art27@cantab.net> * Apply suggestions from code review * remove unused Scopes Signed-off-by: Andrew Thornton <art27@cantab.net> Co-authored-by: techknowlogick <techknowlogick@gitea.io>
1 parent 7e7006e commit ab9bb54

File tree

29 files changed

+2132
-260
lines changed

29 files changed

+2132
-260
lines changed

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,7 @@ github.com/mailru/easyjson v0.7.1/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7
762762
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
763763
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
764764
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
765+
github.com/markbates/going v1.0.0 h1:DQw0ZP7NbNlFGcKbcE/IVSOAFzScxRtLpd0rLMzLhq0=
765766
github.com/markbates/going v1.0.0/go.mod h1:I6mnB4BPnEeqo85ynXIx1ZFLLbtiLHNXVgWeFO9OGOA=
766767
github.com/markbates/goth v1.68.0 h1:90sKvjRAKHcl9V2uC9x/PJXeD78cFPiBsyP1xVhoQfA=
767768
github.com/markbates/goth v1.68.0/go.mod h1:V2VcDMzDiMHW+YmqYl7i0cMiAUeCkAe4QE6jRKBhXZw=

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -2441,6 +2441,7 @@ auths.oauth2_tokenURL = Token URL
24412441
auths.oauth2_authURL = Authorize URL
24422442
auths.oauth2_profileURL = Profile URL
24432443
auths.oauth2_emailURL = Email URL
2444+
auths.oauth2_tenant = Tenant
24442445
auths.enable_auto_register = Enable Auto Registration
24452446
auths.sspi_auto_create_users = Automatically create users
24462447
auths.sspi_auto_create_users_helper = Allow SSPI auth method to automatically create new accounts for users that login for the first time

public/img/auth/azuread.png

3.03 KB
Loading

public/img/auth/azureadv2.png

3.03 KB
Loading

public/img/auth/microsoftonline.png

792 Bytes
Loading

routers/web/admin/auths.go

+21-13
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ func NewAuthSource(ctx *context.Context) {
9898
ctx.Data["AuthSources"] = authSources
9999
ctx.Data["SecurityProtocols"] = securityProtocols
100100
ctx.Data["SMTPAuths"] = smtp.Authenticators
101-
ctx.Data["OAuth2Providers"] = oauth2.Providers
102-
ctx.Data["OAuth2DefaultCustomURLMappings"] = oauth2.DefaultCustomURLMappings
101+
oauth2providers := oauth2.GetOAuth2Providers()
102+
ctx.Data["OAuth2Providers"] = oauth2providers
103103

104104
ctx.Data["SSPIAutoCreateUsers"] = true
105105
ctx.Data["SSPIAutoActivateUsers"] = true
@@ -108,10 +108,7 @@ func NewAuthSource(ctx *context.Context) {
108108
ctx.Data["SSPIDefaultLanguage"] = ""
109109

110110
// only the first as default
111-
for key := range oauth2.Providers {
112-
ctx.Data["oauth2_provider"] = key
113-
break
114-
}
111+
ctx.Data["oauth2_provider"] = oauth2providers[0]
115112

116113
ctx.HTML(http.StatusOK, tplAuthNew)
117114
}
@@ -170,6 +167,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
170167
AuthURL: form.Oauth2AuthURL,
171168
ProfileURL: form.Oauth2ProfileURL,
172169
EmailURL: form.Oauth2EmailURL,
170+
Tenant: form.Oauth2Tenant,
173171
}
174172
} else {
175173
customURLMapping = nil
@@ -220,8 +218,8 @@ func NewAuthSourcePost(ctx *context.Context) {
220218
ctx.Data["AuthSources"] = authSources
221219
ctx.Data["SecurityProtocols"] = securityProtocols
222220
ctx.Data["SMTPAuths"] = smtp.Authenticators
223-
ctx.Data["OAuth2Providers"] = oauth2.Providers
224-
ctx.Data["OAuth2DefaultCustomURLMappings"] = oauth2.DefaultCustomURLMappings
221+
oauth2providers := oauth2.GetOAuth2Providers()
222+
ctx.Data["OAuth2Providers"] = oauth2providers
225223

226224
ctx.Data["SSPIAutoCreateUsers"] = true
227225
ctx.Data["SSPIAutoActivateUsers"] = true
@@ -299,8 +297,8 @@ func EditAuthSource(ctx *context.Context) {
299297

300298
ctx.Data["SecurityProtocols"] = securityProtocols
301299
ctx.Data["SMTPAuths"] = smtp.Authenticators
302-
ctx.Data["OAuth2Providers"] = oauth2.Providers
303-
ctx.Data["OAuth2DefaultCustomURLMappings"] = oauth2.DefaultCustomURLMappings
300+
oauth2providers := oauth2.GetOAuth2Providers()
301+
ctx.Data["OAuth2Providers"] = oauth2providers
304302

305303
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
306304
if err != nil {
@@ -311,7 +309,17 @@ func EditAuthSource(ctx *context.Context) {
311309
ctx.Data["HasTLS"] = source.HasTLS()
312310

313311
if source.IsOAuth2() {
314-
ctx.Data["CurrentOAuth2Provider"] = oauth2.Providers[source.Cfg.(*oauth2.Source).Provider]
312+
type Named interface {
313+
Name() string
314+
}
315+
316+
for _, provider := range oauth2providers {
317+
if provider.Name() == source.Cfg.(Named).Name() {
318+
ctx.Data["CurrentOAuth2Provider"] = provider
319+
break
320+
}
321+
}
322+
315323
}
316324
ctx.HTML(http.StatusOK, tplAuthEdit)
317325
}
@@ -324,8 +332,8 @@ func EditAuthSourcePost(ctx *context.Context) {
324332
ctx.Data["PageIsAdminAuthentications"] = true
325333

326334
ctx.Data["SMTPAuths"] = smtp.Authenticators
327-
ctx.Data["OAuth2Providers"] = oauth2.Providers
328-
ctx.Data["OAuth2DefaultCustomURLMappings"] = oauth2.DefaultCustomURLMappings
335+
oauth2providers := oauth2.GetOAuth2Providers()
336+
ctx.Data["OAuth2Providers"] = oauth2providers
329337

330338
source, err := models.GetLoginSourceByID(ctx.ParamsInt64(":authid"))
331339
if err != nil {

routers/web/user/setting/security.go

+13-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"code.gitea.io/gitea/modules/base"
1313
"code.gitea.io/gitea/modules/context"
1414
"code.gitea.io/gitea/modules/setting"
15-
"code.gitea.io/gitea/services/auth/source/oauth2"
1615
)
1716

1817
const (
@@ -92,9 +91,19 @@ func loadSecurityData(ctx *context.Context) {
9291
for _, externalAccount := range accountLinks {
9392
if loginSource, err := models.GetLoginSourceByID(externalAccount.LoginSourceID); err == nil {
9493
var providerDisplayName string
95-
if loginSource.IsOAuth2() {
96-
providerTechnicalName := loginSource.Cfg.(*oauth2.Source).Provider
97-
providerDisplayName = oauth2.Providers[providerTechnicalName].DisplayName
94+
95+
type DisplayNamed interface {
96+
DisplayName() string
97+
}
98+
99+
type Named interface {
100+
Name() string
101+
}
102+
103+
if displayNamed, ok := loginSource.Cfg.(DisplayNamed); ok {
104+
providerDisplayName = displayNamed.DisplayName()
105+
} else if named, ok := loginSource.Cfg.(Named); ok {
106+
providerDisplayName = named.Name()
98107
} else {
99108
providerDisplayName = loginSource.Name
100109
}

0 commit comments

Comments
 (0)