-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth0_client.go
More file actions
143 lines (115 loc) · 3.59 KB
/
auth0_client.go
File metadata and controls
143 lines (115 loc) · 3.59 KB
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
133
134
135
136
137
138
139
140
141
142
143
package auth0cliauthorizer
import (
"context"
"net/http"
"net/url"
"strings"
"github.com/pkg/errors"
)
const (
defaultScopes = "profile email openid"
scopeOfflineAccess = "offline_access"
)
func (a *DefaultImpl) getDeviceCode(ctx context.Context) (deviceCodeResponseDTO, error) {
a.logger.Debug("requesting a device code")
scopes := defaultScopes
if a.requireOfflineAccess {
scopes += " " + scopeOfflineAccess
}
data := url.Values{}
data.Set("client_id", a.clientID)
data.Set("audience", a.audience)
data.Set("scope", scopes)
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
a.relativeURL("oauth/device/code"),
strings.NewReader(data.Encode()),
)
if err != nil {
return deviceCodeResponseDTO{}, errors.Wrap(err, "error creating HTTP request")
}
var deserialized deviceCodeResponseDTO
if err = a.doWithClient(req, &deserialized); err != nil {
return deviceCodeResponseDTO{}, err
}
if deserialized.DeviceCode == "" {
return deviceCodeResponseDTO{}, errors.New("received an empty device code")
}
if deserialized.VerificationUriComplete == "" {
return deviceCodeResponseDTO{}, errors.New("received an empty verification URI")
}
return deserialized, nil
}
func (a *DefaultImpl) getTokenFromDeviceCode(ctx context.Context, deviceCode string) (tokenResponseDTO, error) {
data := url.Values{}
data.Set("client_id", a.clientID)
data.Set("grant_type", "urn:ietf:params:oauth:grant-type:device_code")
data.Set("device_code", deviceCode)
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
a.relativeURL("oauth/token"),
strings.NewReader(data.Encode()),
)
if err != nil {
return tokenResponseDTO{}, errors.Wrap(err, "error creating HTTP request")
}
var deserialized tokenResponseDTO
if err = a.doWithClient(req, &deserialized); err != nil {
return tokenResponseDTO{}, err
}
if deserialized.AccessToken == "" {
return tokenResponseDTO{}, errors.New("received an empty access token")
}
if deserialized.TokenType == "" {
return tokenResponseDTO{}, errors.New("received an empty token type")
}
return deserialized, nil
}
func (a *DefaultImpl) getUserInfo(ctx context.Context, token string) (userInfoResponseDTO, error) {
req, err := http.NewRequestWithContext(
ctx,
http.MethodGet,
a.relativeURL("userinfo"),
nil,
)
if err != nil {
return userInfoResponseDTO{}, errors.Wrap(err, "error creating HTTP request")
}
req.Header.Set("Authorization", "Bearer "+token)
var deserialized userInfoResponseDTO
if err = a.doWithClient(req, &deserialized); err != nil {
return userInfoResponseDTO{}, err
}
if deserialized.Email == "" {
return userInfoResponseDTO{}, errors.New("received an empty email")
}
return deserialized, nil
}
func (a *DefaultImpl) getTokenFromRefreshToken(ctx context.Context, refreshToken string) (refreshTokenResponseDTO, error) {
data := url.Values{}
data.Set("client_id", a.clientID)
data.Set("grant_type", "refresh_token")
data.Set("refresh_token", refreshToken)
req, err := http.NewRequestWithContext(
ctx,
http.MethodPost,
a.relativeURL("oauth/token"),
strings.NewReader(data.Encode()),
)
if err != nil {
return refreshTokenResponseDTO{}, errors.Wrap(err, "error creating HTTP request")
}
var deserialized refreshTokenResponseDTO
if err = a.doWithClient(req, &deserialized); err != nil {
return refreshTokenResponseDTO{}, err
}
if deserialized.AccessToken == "" {
return refreshTokenResponseDTO{}, errors.New("received an empty access token")
}
if deserialized.TokenType == "" {
return refreshTokenResponseDTO{}, errors.New("received an empty token type")
}
return deserialized, nil
}