Skip to content

Commit 2837563

Browse files
oauth2 with remote Gitea - Fix #8093 (#8149)
1 parent cff0787 commit 2837563

File tree

14 files changed

+307
-17
lines changed

14 files changed

+307
-17
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ require (
6868
github.com/lib/pq v1.2.0
6969
github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96
7070
github.com/lunny/levelqueue v0.0.0-20190217115915-02b525a4418e
71+
github.com/markbates/goth v1.56.0
7172
github.com/mailru/easyjson v0.7.0 // indirect
72-
github.com/markbates/goth v1.49.0
7373
github.com/mattn/go-isatty v0.0.7
7474
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d // indirect
7575
github.com/mattn/go-sqlite3 v1.11.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,8 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN
408408
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
409409
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
410410
github.com/markbates/going v1.0.0/go.mod h1:I6mnB4BPnEeqo85ynXIx1ZFLLbtiLHNXVgWeFO9OGOA=
411-
github.com/markbates/goth v1.49.0 h1:qQ4Ti4WaqAxNAggOC+4s5M85sMVfMJwQn/Xkp73wfgI=
412-
github.com/markbates/goth v1.49.0/go.mod h1:zZmAw0Es0Dpm7TT/4AdN14QrkiWLMrrU9Xei1o+/mdA=
411+
github.com/markbates/goth v1.56.0 h1:XEYedCgMNz5pi3ojXI8z2XUmXtBnMeuKUpx4Z6HlNj8=
412+
github.com/markbates/goth v1.56.0/go.mod h1:zZmAw0Es0Dpm7TT/4AdN14QrkiWLMrrU9Xei1o+/mdA=
413413
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
414414
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
415415
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d h1:m+dSK37rFf2fqppZhg15yI2IwC9BtucBiRwSDm9VL8g=

models/oauth2.go

+8
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ var OAuth2Providers = map[string]OAuth2Provider{
4444
"openidConnect": {Name: "openidConnect", DisplayName: "OpenID Connect", Image: "/img/auth/openid_connect.png"},
4545
"twitter": {Name: "twitter", DisplayName: "Twitter", Image: "/img/auth/twitter.png"},
4646
"discord": {Name: "discord", DisplayName: "Discord", Image: "/img/auth/discord.png"},
47+
"gitea": {Name: "gitea", DisplayName: "Gitea", Image: "/img/auth/gitea.png",
48+
CustomURLMapping: &oauth2.CustomURLMapping{
49+
TokenURL: oauth2.GetDefaultTokenURL("gitea"),
50+
AuthURL: oauth2.GetDefaultAuthURL("gitea"),
51+
ProfileURL: oauth2.GetDefaultProfileURL("gitea"),
52+
},
53+
},
4754
}
4855

4956
// OAuth2DefaultCustomURLMappings contains the map of default URL's for OAuth2 providers that are allowed to have custom urls
@@ -52,6 +59,7 @@ var OAuth2Providers = map[string]OAuth2Provider{
5259
var OAuth2DefaultCustomURLMappings = map[string]*oauth2.CustomURLMapping{
5360
"github": OAuth2Providers["github"].CustomURLMapping,
5461
"gitlab": OAuth2Providers["gitlab"].CustomURLMapping,
62+
"gitea": OAuth2Providers["gitea"].CustomURLMapping,
5563
}
5664

5765
// GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources

modules/auth/oauth2/oauth2.go

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/markbates/goth/providers/discord"
2020
"github.com/markbates/goth/providers/dropbox"
2121
"github.com/markbates/goth/providers/facebook"
22+
"github.com/markbates/goth/providers/gitea"
2223
"github.com/markbates/goth/providers/github"
2324
"github.com/markbates/goth/providers/gitlab"
2425
"github.com/markbates/goth/providers/gplus"
@@ -175,6 +176,22 @@ func createProvider(providerName, providerType, clientID, clientSecret, openIDCo
175176
provider = twitter.NewAuthenticate(clientID, clientSecret, callbackURL)
176177
case "discord":
177178
provider = discord.New(clientID, clientSecret, callbackURL, discord.ScopeIdentify, discord.ScopeEmail)
179+
case "gitea":
180+
authURL := gitea.AuthURL
181+
tokenURL := gitea.TokenURL
182+
profileURL := gitea.ProfileURL
183+
if customURLMapping != nil {
184+
if len(customURLMapping.AuthURL) > 0 {
185+
authURL = customURLMapping.AuthURL
186+
}
187+
if len(customURLMapping.TokenURL) > 0 {
188+
tokenURL = customURLMapping.TokenURL
189+
}
190+
if len(customURLMapping.ProfileURL) > 0 {
191+
profileURL = customURLMapping.ProfileURL
192+
}
193+
}
194+
provider = gitea.NewCustomisedURL(clientID, clientSecret, callbackURL, authURL, tokenURL, profileURL)
178195
}
179196

180197
// always set the name if provider is created so we can support multiple setups of 1 provider
@@ -192,6 +209,8 @@ func GetDefaultTokenURL(provider string) string {
192209
return github.TokenURL
193210
case "gitlab":
194211
return gitlab.TokenURL
212+
case "gitea":
213+
return gitea.TokenURL
195214
}
196215
return ""
197216
}
@@ -203,6 +222,8 @@ func GetDefaultAuthURL(provider string) string {
203222
return github.AuthURL
204223
case "gitlab":
205224
return gitlab.AuthURL
225+
case "gitea":
226+
return gitea.AuthURL
206227
}
207228
return ""
208229
}
@@ -214,6 +235,8 @@ func GetDefaultProfileURL(provider string) string {
214235
return github.ProfileURL
215236
case "gitlab":
216237
return gitlab.ProfileURL
238+
case "gitea":
239+
return gitea.ProfileURL
217240
}
218241
return ""
219242
}

options/locale/locale_en-US.ini

+1
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,7 @@ auths.tip.google_plus = Obtain OAuth2 client credentials from the Google API con
17061706
auths.tip.openid_connect = Use the OpenID Connect Discovery URL (<server>/.well-known/openid-configuration) to specify the endpoints
17071707
auths.tip.twitter = Go to https://dev.twitter.com/apps, create an application and ensure that the “Allow this application to be used to # with Twitter” option is enabled
17081708
auths.tip.discord = Register a new application on https://discordapp.com/developers/applications/me
1709+
auths.tip.gitea = Register a new OAuth2 application. Guide can be found at https://docs.gitea.io/en-us/oauth2-provider/
17091710
auths.edit = Edit Authentication Source
17101711
auths.activated = This Authentication Source is Activated
17111712
auths.new_success = The authentication '%s' has been added.

public/img/auth/gitea.png

5.45 KB
Loading

public/js/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1576,6 +1576,7 @@ function initAdmin() {
15761576
switch (provider) {
15771577
case 'github':
15781578
case 'gitlab':
1579+
case 'gitea':
15791580
$('.oauth2_use_custom_url').show();
15801581
break;
15811582
case 'openidConnect':
@@ -1609,6 +1610,7 @@ function initAdmin() {
16091610
$('.oauth2_token_url input, .oauth2_auth_url input, .oauth2_profile_url input, .oauth2_email_url input').attr('required', 'required');
16101611
$('.oauth2_token_url, .oauth2_auth_url, .oauth2_profile_url, .oauth2_email_url').show();
16111612
break;
1613+
case 'gitea':
16121614
case 'gitlab':
16131615
$('.oauth2_token_url input, .oauth2_auth_url input, .oauth2_profile_url input').attr('required', 'required');
16141616
$('.oauth2_token_url, .oauth2_auth_url, .oauth2_profile_url').show();

templates/admin/auth/new.tmpl

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
<span>{{.i18n.Tr "admin.auths.tip.twitter"}}</span>
111111
<li>Discord</li>
112112
<span>{{.i18n.Tr "admin.auths.tip.discord"}}</span>
113+
<li>Gitea</li>
114+
<span>{{.i18n.Tr "admin.auths.tip.gitea"}}</span>
113115
</div>
114116
</div>
115117
</div>

vendor/github.com/markbates/goth/.travis.yml

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

vendor/github.com/markbates/goth/README.md

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

vendor/github.com/markbates/goth/gothic/gothic.go

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

vendor/github.com/markbates/goth/providers/gitea/gitea.go

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

0 commit comments

Comments
 (0)