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.
[pocketbase#2534] added Instagram OAuth2 provider
Co-authored-by: Pedro Costa <[email protected]>
- Loading branch information
1 parent
728427c
commit a6bb1bf
Showing
9 changed files
with
107 additions
and
4 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
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
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,63 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
|
||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/instagram" | ||
) | ||
|
||
var _ Provider = (*Instagram)(nil) | ||
|
||
// NameInstagram is the unique name of the Instagram provider. | ||
const NameInstagram string = "instagram" | ||
|
||
// Instagram allows authentication via Instagram OAuth2. | ||
type Instagram struct { | ||
*baseProvider | ||
} | ||
|
||
// NewInstagramProvider creates new Instagram provider instance with some defaults. | ||
func NewInstagramProvider() *Instagram { | ||
return &Instagram{&baseProvider{ | ||
ctx: context.Background(), | ||
scopes: []string{"user_profile"}, | ||
authUrl: instagram.Endpoint.AuthURL, | ||
tokenUrl: instagram.Endpoint.TokenURL, | ||
userApiUrl: "https://graph.instagram.com/me?fields=id,username,account_type", | ||
}} | ||
} | ||
|
||
// FetchAuthUser returns an AuthUser instance based on the Instagram's user api. | ||
// | ||
// API reference: https://developers.facebook.com/docs/instagram-basic-display-api/reference/user#fields | ||
func (p *Instagram) 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 string `json:"id"` | ||
Username string `json:"username"` | ||
}{} | ||
if err := json.Unmarshal(data, &extracted); err != nil { | ||
return nil, err | ||
} | ||
|
||
user := &AuthUser{ | ||
Id: extracted.Id, | ||
Username: extracted.Username, | ||
RawUser: rawUser, | ||
AccessToken: token.AccessToken, | ||
RefreshToken: token.RefreshToken, | ||
} | ||
|
||
return user, nil | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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