Skip to content

Commit 7974b34

Browse files
divbhasintechknowlogicklafriks
authored
#12897 - add mastodon provider (#13293)
* added mastodon provider to oauth code * changed go.mod and vendor/modules.txt to add updated goth * vendored mastodon and new goth * committing result of go mod tidy && go mod vendor * added pic and mastodon to oauth models * handled instance url * applied lafriks suggestion * Update web_src/js/index.js Co-authored-by: techknowlogick <[email protected]> Co-authored-by: techknowlogick <[email protected]> Co-authored-by: Lauris BH <[email protected]> Co-authored-by: techknowlogick <[email protected]>
1 parent f565cf5 commit 7974b34

File tree

11 files changed

+327
-1
lines changed

11 files changed

+327
-1
lines changed

go.sum

Lines changed: 54 additions & 0 deletions
Large diffs are not rendered by default.

models/oauth2.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ var OAuth2Providers = map[string]OAuth2Provider{
5959
},
6060
},
6161
"yandex": {Name: "yandex", DisplayName: "Yandex", Image: "/img/auth/yandex.png"},
62+
"mastodon": {Name: "mastodon", DisplayName: "Mastodon", Image: "/img/auth/mastodon.png",
63+
CustomURLMapping: &oauth2.CustomURLMapping{
64+
AuthURL: oauth2.GetDefaultAuthURL("mastodon"),
65+
},
66+
},
6267
}
6368

6469
// OAuth2DefaultCustomURLMappings contains the map of default URL's for OAuth2 providers that are allowed to have custom urls
@@ -69,6 +74,7 @@ var OAuth2DefaultCustomURLMappings = map[string]*oauth2.CustomURLMapping{
6974
"gitlab": OAuth2Providers["gitlab"].CustomURLMapping,
7075
"gitea": OAuth2Providers["gitea"].CustomURLMapping,
7176
"nextcloud": OAuth2Providers["nextcloud"].CustomURLMapping,
77+
"mastodon": OAuth2Providers["mastodon"].CustomURLMapping,
7278
}
7379

7480
// GetActiveOAuth2ProviderLoginSources returns all actived LoginOAuth2 sources

modules/auth/oauth2/oauth2.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"github.com/markbates/goth/providers/github"
2424
"github.com/markbates/goth/providers/gitlab"
2525
"github.com/markbates/goth/providers/google"
26+
"github.com/markbates/goth/providers/mastodon"
2627
"github.com/markbates/goth/providers/nextcloud"
2728
"github.com/markbates/goth/providers/openidConnect"
2829
"github.com/markbates/goth/providers/twitter"
@@ -213,6 +214,12 @@ func createProvider(providerName, providerType, clientID, clientSecret, openIDCo
213214
case "yandex":
214215
// See https://tech.yandex.com/passport/doc/dg/reference/response-docpage/
215216
provider = yandex.New(clientID, clientSecret, callbackURL, "login:email", "login:info", "login:avatar")
217+
case "mastodon":
218+
instanceURL := mastodon.InstanceURL
219+
if customURLMapping != nil && len(customURLMapping.AuthURL) > 0 {
220+
instanceURL = customURLMapping.AuthURL
221+
}
222+
provider = mastodon.NewCustomisedURL(clientID, clientSecret, callbackURL, instanceURL)
216223
}
217224

218225
// always set the name if provider is created so we can support multiple setups of 1 provider
@@ -249,6 +256,8 @@ func GetDefaultAuthURL(provider string) string {
249256
return gitea.AuthURL
250257
case "nextcloud":
251258
return nextcloud.AuthURL
259+
case "mastodon":
260+
return mastodon.InstanceURL
252261
}
253262
return ""
254263
}

options/locale/locale_en-US.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2208,6 +2208,7 @@ auths.tip.twitter = Go to https://dev.twitter.com/apps, create an application an
22082208
auths.tip.discord = Register a new application on https://discordapp.com/developers/applications/me
22092209
auths.tip.gitea = Register a new OAuth2 application. Guide can be found at https://docs.gitea.io/en-us/oauth2-provider/
22102210
auths.tip.yandex = Create a new application at https://oauth.yandex.com/client/new. Select following permissions from the "Yandex.Passport API" section: "Access to email address", "Access to user avatar" and "Access to username, first name and surname, gender"
2211+
auths.tip.mastodon = Input a custom instance URL for the mastodon instance you want to authenticate with (or use the default one)
22112212
auths.edit = Edit Authentication Source
22122213
auths.activated = This Authentication Source is Activated
22132214
auths.new_success = The authentication '%s' has been added.

public/img/auth/mastodon.png

4.99 KB
Loading

templates/admin/auth/new.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@
119119
<span>{{.i18n.Tr "admin.auths.tip.nextcloud"}}</span>
120120
<li>Yandex</li>
121121
<span>{{.i18n.Tr "admin.auths.tip.yandex"}}</span>
122+
<li>Mastodon</li>
123+
<span>{{.i18n.Tr "admin.auths.tip.mastodon"}}</span>
122124
</div>
123125
</div>
124126
</div>

templates/admin/auth/source/oauth.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,4 @@
5555
<input id="{{$key}}_email_url" value="{{$value.EmailURL}}" type="hidden" />
5656
{{end}}
5757
{{end}}
58-
</div>
58+
</div>

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

Lines changed: 185 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/markbates/goth/providers/mastodon/session.go

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ github.com/markbates/goth/providers/gitea
559559
github.com/markbates/goth/providers/github
560560
github.com/markbates/goth/providers/gitlab
561561
github.com/markbates/goth/providers/google
562+
github.com/markbates/goth/providers/mastodon
562563
github.com/markbates/goth/providers/nextcloud
563564
github.com/markbates/goth/providers/openidConnect
564565
github.com/markbates/goth/providers/twitter

web_src/js/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1798,6 +1798,7 @@ function initAdmin() {
17981798
case 'gitlab':
17991799
case 'gitea':
18001800
case 'nextcloud':
1801+
case 'mastodon':
18011802
$('.oauth2_use_custom_url').show();
18021803
break;
18031804
case 'openidConnect':
@@ -1831,6 +1832,10 @@ function initAdmin() {
18311832
$('.oauth2_token_url, .oauth2_auth_url, .oauth2_profile_url').show();
18321833
$('#oauth2_email_url').val('');
18331834
break;
1835+
case 'mastodon':
1836+
$('.oauth2_auth_url input').attr('required', 'required');
1837+
$('.oauth2_auth_url').show();
1838+
break;
18341839
}
18351840
}
18361841
}

0 commit comments

Comments
 (0)