Skip to content

Commit a1058ae

Browse files
committed
Add AzureAD, AzureADv2, MicrosoftOnline OAuth2 providers
Signed-off-by: Andrew Thornton <[email protected]>
1 parent d094cea commit a1058ae

File tree

23 files changed

+1632
-6
lines changed

23 files changed

+1632
-6
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
@@ -2440,6 +2440,7 @@ auths.oauth2_tokenURL = Token URL
24402440
auths.oauth2_authURL = Authorize URL
24412441
auths.oauth2_profileURL = Profile URL
24422442
auths.oauth2_emailURL = Email URL
2443+
auths.oauth2_tenant = Tenant
24432444
auths.enable_auto_register = Enable Auto Registration
24442445
auths.sspi_auto_create_users = Automatically create users
24452446
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

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ func parseOAuth2Config(form forms.AuthenticationForm) *oauth2.Source {
167167
AuthURL: form.Oauth2AuthURL,
168168
ProfileURL: form.Oauth2ProfileURL,
169169
EmailURL: form.Oauth2EmailURL,
170+
Tenant: form.Oauth2Tenant,
170171
}
171172
} else {
172173
customURLMapping = nil

services/auth/source/oauth2/providers_custom.go

+13
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package oauth2
77
import (
88
"code.gitea.io/gitea/modules/setting"
99
"github.com/markbates/goth"
10+
"github.com/markbates/goth/providers/azureadv2"
1011
"github.com/markbates/goth/providers/gitea"
1112
"github.com/markbates/goth/providers/github"
1213
"github.com/markbates/goth/providers/gitlab"
@@ -65,6 +66,7 @@ func init() {
6566
}
6667
return github.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL, custom.TokenURL, custom.ProfileURL, custom.EmailURL, scopes...), nil
6768
}))
69+
6870
RegisterGothProvider(NewCustomProvider(
6971
"gitlab", "GitLab", &CustomURLSettings{
7072
AuthURL: availableAttribute(gitlab.AuthURL),
@@ -101,4 +103,15 @@ func init() {
101103
func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
102104
return mastodon.NewCustomisedURL(clientID, secret, callbackURL, custom.AuthURL), nil
103105
}))
106+
107+
RegisterGothProvider(NewCustomProvider(
108+
"azureadv2", "Azure AD v2", &CustomURLSettings{
109+
Tenant: requiredAttribute("organizations"),
110+
},
111+
func(clientID, secret, callbackURL string, custom *CustomURLMapping) (goth.Provider, error) {
112+
return azureadv2.New(clientID, secret, callbackURL, azureadv2.ProviderOptions{
113+
Tenant: azureadv2.TenantType(custom.Tenant),
114+
}), nil
115+
},
116+
))
104117
}

services/auth/source/oauth2/providers_simple.go

+16
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ package oauth2
77
import (
88
"code.gitea.io/gitea/modules/setting"
99
"github.com/markbates/goth"
10+
"github.com/markbates/goth/providers/azuread"
1011
"github.com/markbates/goth/providers/bitbucket"
1112
"github.com/markbates/goth/providers/discord"
1213
"github.com/markbates/goth/providers/dropbox"
1314
"github.com/markbates/goth/providers/facebook"
1415
"github.com/markbates/goth/providers/google"
16+
"github.com/markbates/goth/providers/microsoftonline"
1517
"github.com/markbates/goth/providers/twitter"
1618
"github.com/markbates/goth/providers/yandex"
1719
)
@@ -88,4 +90,18 @@ func init() {
8890
return yandex.New(clientKey, secret, callbackURL, scopes...)
8991
}))
9092

93+
RegisterGothProvider(NewSimpleProvider(
94+
"azuread", "Azure AD", nil,
95+
func(clientID, secret, callbackURL string, scopes ...string) goth.Provider {
96+
return azuread.New(clientID, secret, callbackURL, nil, scopes...)
97+
},
98+
))
99+
100+
RegisterGothProvider(NewSimpleProvider(
101+
"microsoftonline", "Microsoft Online", nil,
102+
func(clientID, secret, callbackURL string, scopes ...string) goth.Provider {
103+
return microsoftonline.New(clientID, secret, callbackURL, scopes...)
104+
},
105+
))
106+
91107
}

services/auth/source/oauth2/urlmapping.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ type CustomURLMapping struct {
1010
TokenURL string `json:",omitempty"`
1111
ProfileURL string `json:",omitempty"`
1212
EmailURL string `json:",omitempty"`
13+
Tenant string `json:",omitempty"`
1314
}
1415

1516
// CustomURLSettings describes the urls values and availability to use when customizing OAuth2 provider URLs
@@ -18,6 +19,7 @@ type CustomURLSettings struct {
1819
TokenURL Attribute `json:",omitempty"`
1920
ProfileURL Attribute `json:",omitempty"`
2021
EmailURL Attribute `json:",omitempty"`
22+
Tenant Attribute `json:",omitempty"`
2123
}
2224

2325
// Attribute describes the availability, and required status for a custom url configuration
@@ -40,7 +42,7 @@ func (c *CustomURLSettings) Required() bool {
4042
if c == nil {
4143
return false
4244
}
43-
if c.AuthURL.Required || c.EmailURL.Required || c.ProfileURL.Required || c.TokenURL.Required {
45+
if c.AuthURL.Required || c.EmailURL.Required || c.ProfileURL.Required || c.TokenURL.Required || c.Tenant.Required {
4446
return true
4547
}
4648
return false
@@ -53,6 +55,7 @@ func (c *CustomURLSettings) OverrideWith(override *CustomURLMapping) *CustomURLM
5355
TokenURL: c.TokenURL.Value,
5456
ProfileURL: c.ProfileURL.Value,
5557
EmailURL: c.EmailURL.Value,
58+
Tenant: c.Tenant.Value,
5659
}
5760
if override != nil {
5861
if len(override.AuthURL) > 0 && c.AuthURL.Available {
@@ -67,6 +70,9 @@ func (c *CustomURLSettings) OverrideWith(override *CustomURLMapping) *CustomURLM
6770
if len(override.EmailURL) > 0 && c.EmailURL.Available {
6871
custom.EmailURL = override.EmailURL
6972
}
73+
if len(override.Tenant) > 0 && c.Tenant.Available {
74+
custom.Tenant = override.Tenant
75+
}
7076
}
7177
return custom
7278
}

services/forms/auth_form.go

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type AuthenticationForm struct {
6262
Oauth2ProfileURL string
6363
Oauth2EmailURL string
6464
Oauth2IconURL string
65+
Oauth2Tenant string
6566
SSPIAutoCreateUsers bool
6667
SSPIAutoActivateUsers bool
6768
SSPIStripDomainNames bool

templates/admin/auth/edit.tmpl

+6
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,18 @@
248248
<label for="oauth2_email_url">{{.i18n.Tr "admin.auths.oauth2_emailURL"}}</label>
249249
<input id="oauth2_email_url" name="oauth2_email_url" value="{{if $cfg.CustomURLMapping}}{{$cfg.CustomURLMapping.EmailURL}}{{end}}">
250250
</div>
251+
<div class="oauth2_use_custom_url_field oauth2_tenant required field">
252+
<label for="oauth2_tenant">{{.i18n.Tr "admin.auths.oauth2_tenant"}}</label>
253+
<input id="oauth2_tenant" name="oauth2_tenant" value="{{if $cfg.CustomURLMapping}}{{$cfg.CustomURLMapping.Tenant}}{{end}}">
254+
</div>
255+
251256
{{range .OAuth2Providers}}{{if .CustomURLSettings}}
252257
<input id="{{.Name}}_customURLSettings" type="hidden" data-required="{{.CustomURLSettings.Required}}" data-available="true">
253258
<input id="{{.Name}}_token_url" value="{{.CustomURLSettings.TokenURL.Value}}" data-available="{{.CustomURLSettings.TokenURL.Available}}" data-required="{{.CustomURLSettings.TokenURL.Required}}" type="hidden" />
254259
<input id="{{.Name}}_auth_url" value="{{.CustomURLSettings.AuthURL.Value}}" data-available="{{.CustomURLSettings.AuthURL.Available}}" data-required="{{.CustomURLSettings.AuthURL.Required}}" type="hidden" />
255260
<input id="{{.Name}}_profile_url" value="{{.CustomURLSettings.ProfileURL.Value}}" data-available="{{.CustomURLSettings.ProfileURL.Available}}" data-required="{{.CustomURLSettings.ProfileURL.Required}}" type="hidden" />
256261
<input id="{{.Name}}_email_url" value="{{.CustomURLSettings.EmailURL.Value}}" data-available="{{.CustomURLSettings.EmailURL.Available}}" data-required="{{.CustomURLSettings.EmailURL.Required}}" type="hidden" />
262+
<input id="{{.Name}}_tenant" value="{{.CustomURLSettings.Tenant.Value}}" data-available="{{.CustomURLSettings.Tenant.Available}}" data-required="{{.CustomURLSettings.Tenant.Required}}" type="hidden" />
257263
{{end}}{{end}}
258264
{{end}}
259265

templates/admin/auth/source/oauth.tmpl

+6
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,17 @@
5151
<label for="oauth2_email_url">{{.i18n.Tr "admin.auths.oauth2_emailURL"}}</label>
5252
<input id="oauth2_email_url" name="oauth2_email_url" value="{{.oauth2_email_url}}">
5353
</div>
54+
<div class="oauth2_use_custom_url_field oauth2_tenant required field">
55+
<label for="oauth2_tenant">{{.i18n.Tr "admin.auths.oauth2_tenant"}}</label>
56+
<input id="oauth2_tenant" name="oauth2_tenant" value="{{.oauth2_tenant}}">
57+
</div>
58+
5459
{{range .OAuth2Providers}}{{if .CustomURLSettings}}
5560
<input id="{{.Name}}_customURLSettings" type="hidden" data-required="{{.CustomURLSettings.Required}}" data-available="true">
5661
<input id="{{.Name}}_token_url" value="{{.CustomURLSettings.TokenURL.Value}}" data-available="{{.CustomURLSettings.TokenURL.Available}}" data-required="{{.CustomURLSettings.TokenURL.Required}}" type="hidden" />
5762
<input id="{{.Name}}_auth_url" value="{{.CustomURLSettings.AuthURL.Value}}" data-available="{{.CustomURLSettings.AuthURL.Available}}" data-required="{{.CustomURLSettings.AuthURL.Required}}" type="hidden" />
5863
<input id="{{.Name}}_profile_url" value="{{.CustomURLSettings.ProfileURL.Value}}" data-available="{{.CustomURLSettings.ProfileURL.Available}}" data-required="{{.CustomURLSettings.ProfileURL.Required}}" type="hidden" />
5964
<input id="{{.Name}}_email_url" value="{{.CustomURLSettings.EmailURL.Value}}" data-available="{{.CustomURLSettings.EmailURL.Available}}" data-required="{{.CustomURLSettings.EmailURL.Required}}" type="hidden" />
65+
<input id="{{.Name}}_tenant" value="{{.CustomURLSettings.Tenant.Value}}" data-available="{{.CustomURLSettings.Tenant.Available}}" data-required="{{.CustomURLSettings.Tenant.Required}}" type="hidden" />
6066
{{end}}{{end}}
6167
</div>

vendor/github.com/markbates/going/LICENSE.txt

+22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/markbates/going/defaults/defaults.go

+36
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)