forked from pocketbase/pocketbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement strava oauth2 as new auth provider
- Loading branch information
Showing
35 changed files
with
137 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,8 @@ func TestSettingsValidate(t *testing.T) { | |
s.KakaoAuth.ClientId = "" | ||
s.TwitchAuth.Enabled = true | ||
s.TwitchAuth.ClientId = "" | ||
s.StravaAuth.Enabled = true | ||
s.StravaAuth.ClientId = "" | ||
|
||
// check if Validate() is triggering the members validate methods. | ||
err := s.Validate() | ||
|
@@ -76,6 +78,7 @@ func TestSettingsValidate(t *testing.T) { | |
`"spotifyAuth":{`, | ||
`"kakaoAuth":{`, | ||
`"twitchAuth":{`, | ||
`"stravaAuth":{`, | ||
} | ||
|
||
errBytes, _ := json.Marshal(err) | ||
|
@@ -124,6 +127,8 @@ func TestSettingsMerge(t *testing.T) { | |
s2.KakaoAuth.ClientId = "kakao_test" | ||
s2.TwitchAuth.Enabled = true | ||
s2.TwitchAuth.ClientId = "twitch_test" | ||
s2.StravaAuth.Enabled = true | ||
s2.StravaAuth.ClientId = "strava_test" | ||
|
||
if err := s1.Merge(s2); err != nil { | ||
t.Fatal(err) | ||
|
@@ -195,6 +200,7 @@ func TestSettingsRedactClone(t *testing.T) { | |
s1.SpotifyAuth.ClientSecret = "test123" | ||
s1.KakaoAuth.ClientSecret = "test123" | ||
s1.TwitchAuth.ClientSecret = "test123" | ||
s1.StravaAuth.ClientSecret = "test123" | ||
|
||
s2, err := s1.RedactClone() | ||
if err != nil { | ||
|
@@ -206,7 +212,7 @@ func TestSettingsRedactClone(t *testing.T) { | |
t.Fatal(err) | ||
} | ||
|
||
expected := `{"meta":{"appName":"test123","appUrl":"http://localhost:8090","hideControls":false,"senderName":"Support","senderAddress":"[email protected]","verificationTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eThank you for joining us at {APP_NAME}.\u003c/p\u003e\n\u003cp\u003eClick on the button below to verify your email address.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eVerify\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Verify your {APP_NAME} email","actionUrl":"{APP_URL}/_/#/auth/confirm-verification/{TOKEN}"},"resetPasswordTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eClick on the button below to reset your password.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eReset password\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\u003ci\u003eIf you didn't ask to reset your password, you can ignore this email.\u003c/i\u003e\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Reset your {APP_NAME} password","actionUrl":"{APP_URL}/_/#/auth/confirm-password-reset/{TOKEN}"},"confirmEmailChangeTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eClick on the button below to confirm your new email address.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eConfirm new email\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\u003ci\u003eIf you didn't ask to change your email address, you can ignore this email.\u003c/i\u003e\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Confirm your {APP_NAME} new email address","actionUrl":"{APP_URL}/_/#/auth/confirm-email-change/{TOKEN}"}},"logs":{"maxDays":5},"smtp":{"enabled":false,"host":"smtp.example.com","port":587,"username":"","password":"******","authMethod":"","tls":true},"s3":{"enabled":false,"bucket":"","region":"","endpoint":"","accessKey":"","secret":"******","forcePathStyle":false},"adminAuthToken":{"secret":"******","duration":1209600},"adminPasswordResetToken":{"secret":"******","duration":1800},"recordAuthToken":{"secret":"******","duration":1209600},"recordPasswordResetToken":{"secret":"******","duration":1800},"recordEmailChangeToken":{"secret":"******","duration":1800},"recordVerificationToken":{"secret":"******","duration":604800},"emailAuth":{"enabled":false,"exceptDomains":null,"onlyDomains":null,"minPasswordLength":0},"googleAuth":{"enabled":false,"clientSecret":"******"},"facebookAuth":{"enabled":false,"clientSecret":"******"},"githubAuth":{"enabled":false,"clientSecret":"******"},"gitlabAuth":{"enabled":false,"clientSecret":"******"},"discordAuth":{"enabled":false,"clientSecret":"******"},"twitterAuth":{"enabled":false,"clientSecret":"******"},"microsoftAuth":{"enabled":false,"clientSecret":"******"},"spotifyAuth":{"enabled":false,"clientSecret":"******"},"kakaoAuth":{"enabled":false,"clientSecret":"******"},"twitchAuth":{"enabled":false,"clientSecret":"******"}}` | ||
expected := `{"meta":{"appName":"test123","appUrl":"http://localhost:8090","hideControls":false,"senderName":"Support","senderAddress":"[email protected]","verificationTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eThank you for joining us at {APP_NAME}.\u003c/p\u003e\n\u003cp\u003eClick on the button below to verify your email address.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eVerify\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Verify your {APP_NAME} email","actionUrl":"{APP_URL}/_/#/auth/confirm-verification/{TOKEN}"},"resetPasswordTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eClick on the button below to reset your password.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eReset password\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\u003ci\u003eIf you didn't ask to reset your password, you can ignore this email.\u003c/i\u003e\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Reset your {APP_NAME} password","actionUrl":"{APP_URL}/_/#/auth/confirm-password-reset/{TOKEN}"},"confirmEmailChangeTemplate":{"body":"\u003cp\u003eHello,\u003c/p\u003e\n\u003cp\u003eClick on the button below to confirm your new email address.\u003c/p\u003e\n\u003cp\u003e\n \u003ca class=\"btn\" href=\"{ACTION_URL}\" target=\"_blank\" rel=\"noopener\"\u003eConfirm new email\u003c/a\u003e\n\u003c/p\u003e\n\u003cp\u003e\u003ci\u003eIf you didn't ask to change your email address, you can ignore this email.\u003c/i\u003e\u003c/p\u003e\n\u003cp\u003e\n Thanks,\u003cbr/\u003e\n {APP_NAME} team\n\u003c/p\u003e","subject":"Confirm your {APP_NAME} new email address","actionUrl":"{APP_URL}/_/#/auth/confirm-email-change/{TOKEN}"}},"logs":{"maxDays":5},"smtp":{"enabled":false,"host":"smtp.example.com","port":587,"username":"","password":"******","authMethod":"","tls":true},"s3":{"enabled":false,"bucket":"","region":"","endpoint":"","accessKey":"","secret":"******","forcePathStyle":false},"adminAuthToken":{"secret":"******","duration":1209600},"adminPasswordResetToken":{"secret":"******","duration":1800},"recordAuthToken":{"secret":"******","duration":1209600},"recordPasswordResetToken":{"secret":"******","duration":1800},"recordEmailChangeToken":{"secret":"******","duration":1800},"recordVerificationToken":{"secret":"******","duration":604800},"emailAuth":{"enabled":false,"exceptDomains":null,"onlyDomains":null,"minPasswordLength":0},"googleAuth":{"enabled":false,"clientSecret":"******"},"facebookAuth":{"enabled":false,"clientSecret":"******"},"githubAuth":{"enabled":false,"clientSecret":"******"},"gitlabAuth":{"enabled":false,"clientSecret":"******"},"discordAuth":{"enabled":false,"clientSecret":"******"},"twitterAuth":{"enabled":false,"clientSecret":"******"},"microsoftAuth":{"enabled":false,"clientSecret":"******"},"spotifyAuth":{"enabled":false,"clientSecret":"******"},"kakaoAuth":{"enabled":false,"clientSecret":"******"},"twitchAuth":{"enabled":false,"clientSecret":"******"},"stravaAuth":{"enabled":false,"clientSecret":"******"}}` | ||
|
||
if encodedStr := string(encoded); encodedStr != expected { | ||
t.Fatalf("Expected\n%v\ngot\n%v", expected, encodedStr) | ||
|
@@ -227,6 +233,7 @@ func TestNamedAuthProviderConfigs(t *testing.T) { | |
s.SpotifyAuth.ClientId = "spotify_test" | ||
s.KakaoAuth.ClientId = "kakao_test" | ||
s.TwitchAuth.ClientId = "twitch_test" | ||
s.StravaAuth.ClientId = "strava_test" | ||
|
||
result := s.NamedAuthProviderConfigs() | ||
|
||
|
@@ -247,6 +254,7 @@ func TestNamedAuthProviderConfigs(t *testing.T) { | |
`"twitter":{"enabled":false,"clientId":"twitter_test"}`, | ||
`"kakao":{"enabled":false,"clientId":"kakao_test"}`, | ||
`"twitch":{"enabled":false,"clientId":"twitch_test"}`, | ||
`"strava":{"enabled":false,"clientId":"strava_test"}`, | ||
} | ||
for _, p := range expectedParts { | ||
if !strings.Contains(encodedStr, p) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package auth | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
|
||
"golang.org/x/oauth2" | ||
) | ||
|
||
var _ Provider = (*Strava)(nil) | ||
|
||
// NameStrava is the unique name of the Strava provider. | ||
const NameStrava string = "strava" | ||
|
||
// Strava allows authentication via Strava OAuth2. | ||
type Strava struct { | ||
*baseProvider | ||
} | ||
|
||
// NewStravaProvider creates new Strava provider instance with some defaults. | ||
func NewStravaProvider() *Strava { | ||
return &Strava{&baseProvider{ | ||
scopes: []string{ | ||
"profile:read_all", | ||
}, | ||
authUrl: "https://www.strava.com/oauth/authorize", | ||
tokenUrl: "https://www.strava.com/api/v3/oauth/token", | ||
userApiUrl: "https://www.strava.com/api/v3/athlete", | ||
}} | ||
} | ||
|
||
// FetchAuthUser returns an AuthUser instance based on the Strava's user api. | ||
// | ||
// API reference: https://developers.strava.com/docs/authentication/ | ||
func (p *Strava) FetchAuthUser(token *oauth2.Token) (*AuthUser, error) { | ||
data, err := p.FetchRawUserData(token) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
rawUser := map[string]any{} | ||
if err := json.Unmarshal(data, &rawUser); err != nil { | ||
return nil, err | ||
} | ||
|
||
extracted := struct { | ||
Id int `json:"id"` | ||
FirstName string `json:"firstname"` | ||
LastName string `json:"lastname"` | ||
Username string `json:"username"` | ||
ProfileImageUrl string `json:"profile"` | ||
|
||
// At the time of writing, Strava OAuth2 doesn't support returning the user email address | ||
// Email string `json:"email"` | ||
}{} | ||
if err := json.Unmarshal(data, &extracted); err != nil { | ||
return nil, err | ||
} | ||
|
||
user := &AuthUser{ | ||
Id: strconv.Itoa(extracted.Id), | ||
Name: extracted.FirstName + " " + extracted.LastName, | ||
Username: extracted.Username, | ||
AvatarUrl: extracted.ProfileImageUrl, | ||
RawUser: rawUser, | ||
AccessToken: token.AccessToken, | ||
} | ||
|
||
return user, nil | ||
} |
2 changes: 1 addition & 1 deletion
2
ui/dist/assets/AuthMethodsDocs.6f7645f7.js → ui/dist/assets/AuthMethodsDocs.7503f9be.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
ui/dist/assets/AuthRefreshDocs.4e70d24d.js → ui/dist/assets/AuthRefreshDocs.6ef16a22.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.