/
provider_vk.go
132 lines (108 loc) · 3.25 KB
/
provider_vk.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package oidc
import (
"context"
"encoding/json"
"net/url"
"strconv"
"github.com/hashicorp/go-retryablehttp"
"github.com/ory/x/httpx"
"github.com/pkg/errors"
"golang.org/x/oauth2"
"github.com/ory/herodot"
)
type ProviderVK struct {
config *Configuration
reg Dependencies
}
func NewProviderVK(
config *Configuration,
reg Dependencies,
) Provider {
return &ProviderVK{
config: config,
reg: reg,
}
}
func (g *ProviderVK) Config() *Configuration {
return g.config
}
func (g *ProviderVK) oauth2(ctx context.Context) *oauth2.Config {
return &oauth2.Config{
ClientID: g.config.ClientID,
ClientSecret: g.config.ClientSecret,
Endpoint: oauth2.Endpoint{
AuthURL: "https://oauth.vk.com/authorize",
TokenURL: "https://oauth.vk.com/access_token",
},
Scopes: g.config.Scope,
RedirectURL: g.config.Redir(g.reg.Config().OIDCRedirectURIBase(ctx)),
}
}
func (g *ProviderVK) AuthCodeURLOptions(r ider) []oauth2.AuthCodeOption {
return []oauth2.AuthCodeOption{}
}
func (g *ProviderVK) OAuth2(ctx context.Context) (*oauth2.Config, error) {
return g.oauth2(ctx), nil
}
func (g *ProviderVK) Claims(ctx context.Context, exchange *oauth2.Token, query url.Values) (*Claims, error) {
o, err := g.OAuth2(ctx)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
ctx, client := httpx.SetOAuth2(ctx, g.reg.HTTPClient(ctx), o, exchange)
req, err := retryablehttp.NewRequestWithContext(ctx, "GET", "https://api.vk.com/method/users.get?fields=photo_200,nickname,bdate,sex&access_token="+exchange.AccessToken+"&v=5.103", nil)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
resp, err := client.Do(req)
if err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
defer resp.Body.Close()
if err := logUpstreamError(g.reg.Logger(), resp); err != nil {
return nil, err
}
type User struct {
Id int `json:"id,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Nickname string `json:"nickname,omitempty"`
Picture string `json:"photo_200,omitempty"`
Email string `json:"-"`
Gender int `json:"sex,omitempty"`
BirthDay string `json:"bdate,omitempty"`
}
var response struct {
Result []User `json:"response,omitempty"`
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("%s", err))
}
if len(response.Result) == 0 {
return nil, errors.WithStack(herodot.ErrInternalServerError.WithReasonf("VK did not return a user in the userinfo request."))
}
user := response.Result[0]
if email, ok := exchange.Extra("email").(string); ok {
user.Email = email
}
gender := ""
switch user.Gender {
case 1:
gender = "female"
case 2:
gender = "male"
}
return &Claims{
Issuer: "https://api.vk.com/method/users.get",
Subject: strconv.Itoa(user.Id),
GivenName: user.FirstName,
FamilyName: user.LastName,
Nickname: user.Nickname,
Picture: user.Picture,
Email: user.Email,
Gender: gender,
Birthdate: user.BirthDay,
}, nil
}