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#2533] added VK OAuth2 provider
Co-authored-by: Valentine <[email protected]>
- Loading branch information
1 parent
e40cf46
commit af71b63
Showing
41 changed files
with
173 additions
and
39 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
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,85 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
|
||
"golang.org/x/oauth2" | ||
"golang.org/x/oauth2/vk" | ||
) | ||
|
||
var _ Provider = (*VK)(nil) | ||
|
||
// NameVK is the unique name of the VK provider. | ||
const NameVK string = "vk" | ||
|
||
// VK allows authentication via VK OAuth2. | ||
type VK struct { | ||
*baseProvider | ||
} | ||
|
||
// NewVKProvider creates new VK provider instance with some defaults. | ||
// | ||
// Docs: https://dev.vk.com/api/oauth-parameters | ||
func NewVKProvider() *VK { | ||
return &VK{&baseProvider{ | ||
ctx: context.Background(), | ||
scopes: []string{"email"}, | ||
authUrl: vk.Endpoint.AuthURL, | ||
tokenUrl: vk.Endpoint.TokenURL, | ||
userApiUrl: "https://api.vk.com/method/users.get?fields=photo_max,screen_name&v=5.131", | ||
}} | ||
} | ||
|
||
// FetchAuthUser returns an AuthUser instance based on VK's user api. | ||
// | ||
// API reference: https://dev.vk.com/method/users.get | ||
func (p *VK) 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 { | ||
Response []struct { | ||
Id int `json:"id"` | ||
FirstName string `json:"first_name"` | ||
LastName string `json:"last_name"` | ||
Username string `json:"screen_name"` | ||
AvatarUrl string `json:"photo_max"` | ||
} `json:"response"` | ||
}{} | ||
|
||
if err := json.Unmarshal(data, &extracted); err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(extracted.Response) == 0 { | ||
return nil, errors.New("missing response entry") | ||
} | ||
|
||
user := &AuthUser{ | ||
Id: strconv.Itoa(extracted.Response[0].Id), | ||
Name: strings.TrimSpace(extracted.Response[0].FirstName + " " + extracted.Response[0].LastName), | ||
Username: extracted.Response[0].Username, | ||
AvatarUrl: extracted.Response[0].AvatarUrl, | ||
RawUser: rawUser, | ||
AccessToken: token.AccessToken, | ||
RefreshToken: token.RefreshToken, | ||
} | ||
|
||
if email := token.Extra("email"); email != nil { | ||
user.Email = fmt.Sprint(email) | ||
} | ||
|
||
return user, nil | ||
} |
2 changes: 1 addition & 1 deletion
2
ui/dist/assets/AuthMethodsDocs-7806e252.js → ui/dist/assets/AuthMethodsDocs-3a6a4650.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-d73a0482.js → ui/dist/assets/AuthRefreshDocs-60b9c81d.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
...ist/assets/AuthWithOAuth2Docs-d466760f.js → ...ist/assets/AuthWithOAuth2Docs-66b72350.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.