Skip to content

Commit

Permalink
oauth2 with remote Gitea - Fix #8093 (#8149)
Browse files Browse the repository at this point in the history
  • Loading branch information
techknowlogick authored Sep 13, 2019
1 parent cff0787 commit 2837563
Show file tree
Hide file tree
Showing 14 changed files with 307 additions and 17 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ require (
github.com/lib/pq v1.2.0
github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96
github.com/lunny/levelqueue v0.0.0-20190217115915-02b525a4418e
github.com/markbates/goth v1.56.0
github.com/mailru/easyjson v0.7.0 // indirect
github.com/markbates/goth v1.49.0
github.com/mattn/go-isatty v0.0.7
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d // indirect
github.com/mattn/go-sqlite3 v1.11.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN
github.com/mailru/easyjson v0.7.0 h1:aizVhC/NAAcKWb+5QsU1iNOZb4Yws5UO2I+aIprQITM=
github.com/mailru/easyjson v0.7.0/go.mod h1:KAzv3t3aY1NaHWoQz1+4F1ccyAH66Jk7yos7ldAVICs=
github.com/markbates/going v1.0.0/go.mod h1:I6mnB4BPnEeqo85ynXIx1ZFLLbtiLHNXVgWeFO9OGOA=
github.com/markbates/goth v1.49.0 h1:qQ4Ti4WaqAxNAggOC+4s5M85sMVfMJwQn/Xkp73wfgI=
github.com/markbates/goth v1.49.0/go.mod h1:zZmAw0Es0Dpm7TT/4AdN14QrkiWLMrrU9Xei1o+/mdA=
github.com/markbates/goth v1.56.0 h1:XEYedCgMNz5pi3ojXI8z2XUmXtBnMeuKUpx4Z6HlNj8=
github.com/markbates/goth v1.56.0/go.mod h1:zZmAw0Es0Dpm7TT/4AdN14QrkiWLMrrU9Xei1o+/mdA=
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-oci8 v0.0.0-20190320171441-14ba190cf52d h1:m+dSK37rFf2fqppZhg15yI2IwC9BtucBiRwSDm9VL8g=
Expand Down
8 changes: 8 additions & 0 deletions models/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ var OAuth2Providers = map[string]OAuth2Provider{
"openidConnect": {Name: "openidConnect", DisplayName: "OpenID Connect", Image: "/img/auth/openid_connect.png"},
"twitter": {Name: "twitter", DisplayName: "Twitter", Image: "/img/auth/twitter.png"},
"discord": {Name: "discord", DisplayName: "Discord", Image: "/img/auth/discord.png"},
"gitea": {Name: "gitea", DisplayName: "Gitea", Image: "/img/auth/gitea.png",
CustomURLMapping: &oauth2.CustomURLMapping{
TokenURL: oauth2.GetDefaultTokenURL("gitea"),
AuthURL: oauth2.GetDefaultAuthURL("gitea"),
ProfileURL: oauth2.GetDefaultProfileURL("gitea"),
},
},
}

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

// GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources
Expand Down
23 changes: 23 additions & 0 deletions modules/auth/oauth2/oauth2.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/markbates/goth/providers/discord"
"github.com/markbates/goth/providers/dropbox"
"github.com/markbates/goth/providers/facebook"
"github.com/markbates/goth/providers/gitea"
"github.com/markbates/goth/providers/github"
"github.com/markbates/goth/providers/gitlab"
"github.com/markbates/goth/providers/gplus"
Expand Down Expand Up @@ -175,6 +176,22 @@ func createProvider(providerName, providerType, clientID, clientSecret, openIDCo
provider = twitter.NewAuthenticate(clientID, clientSecret, callbackURL)
case "discord":
provider = discord.New(clientID, clientSecret, callbackURL, discord.ScopeIdentify, discord.ScopeEmail)
case "gitea":
authURL := gitea.AuthURL
tokenURL := gitea.TokenURL
profileURL := gitea.ProfileURL
if customURLMapping != nil {
if len(customURLMapping.AuthURL) > 0 {
authURL = customURLMapping.AuthURL
}
if len(customURLMapping.TokenURL) > 0 {
tokenURL = customURLMapping.TokenURL
}
if len(customURLMapping.ProfileURL) > 0 {
profileURL = customURLMapping.ProfileURL
}
}
provider = gitea.NewCustomisedURL(clientID, clientSecret, callbackURL, authURL, tokenURL, profileURL)
}

// always set the name if provider is created so we can support multiple setups of 1 provider
Expand All @@ -192,6 +209,8 @@ func GetDefaultTokenURL(provider string) string {
return github.TokenURL
case "gitlab":
return gitlab.TokenURL
case "gitea":
return gitea.TokenURL
}
return ""
}
Expand All @@ -203,6 +222,8 @@ func GetDefaultAuthURL(provider string) string {
return github.AuthURL
case "gitlab":
return gitlab.AuthURL
case "gitea":
return gitea.AuthURL
}
return ""
}
Expand All @@ -214,6 +235,8 @@ func GetDefaultProfileURL(provider string) string {
return github.ProfileURL
case "gitlab":
return gitlab.ProfileURL
case "gitea":
return gitea.ProfileURL
}
return ""
}
Expand Down
1 change: 1 addition & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1706,6 +1706,7 @@ auths.tip.google_plus = Obtain OAuth2 client credentials from the Google API con
auths.tip.openid_connect = Use the OpenID Connect Discovery URL (<server>/.well-known/openid-configuration) to specify the endpoints
auths.tip.twitter = Go to https://dev.twitter.com/apps, create an application and ensure that the “Allow this application to be used to Sign in with Twitter” option is enabled
auths.tip.discord = Register a new application on https://discordapp.com/developers/applications/me
auths.tip.gitea = Register a new OAuth2 application. Guide can be found at https://docs.gitea.io/en-us/oauth2-provider/
auths.edit = Edit Authentication Source
auths.activated = This Authentication Source is Activated
auths.new_success = The authentication '%s' has been added.
Expand Down
Binary file added public/img/auth/gitea.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions public/js/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,7 @@ function initAdmin() {
switch (provider) {
case 'github':
case 'gitlab':
case 'gitea':
$('.oauth2_use_custom_url').show();
break;
case 'openidConnect':
Expand Down Expand Up @@ -1609,6 +1610,7 @@ function initAdmin() {
$('.oauth2_token_url input, .oauth2_auth_url input, .oauth2_profile_url input, .oauth2_email_url input').attr('required', 'required');
$('.oauth2_token_url, .oauth2_auth_url, .oauth2_profile_url, .oauth2_email_url').show();
break;
case 'gitea':
case 'gitlab':
$('.oauth2_token_url input, .oauth2_auth_url input, .oauth2_profile_url input').attr('required', 'required');
$('.oauth2_token_url, .oauth2_auth_url, .oauth2_profile_url').show();
Expand Down
2 changes: 2 additions & 0 deletions templates/admin/auth/new.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@
<span>{{.i18n.Tr "admin.auths.tip.twitter"}}</span>
<li>Discord</li>
<span>{{.i18n.Tr "admin.auths.tip.discord"}}</span>
<li>Gitea</li>
<span>{{.i18n.Tr "admin.auths.tip.gitea"}}</span>
</div>
</div>
</div>
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2837563

Please sign in to comment.