-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoauth.go
174 lines (138 loc) · 4.16 KB
/
oauth.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package vivawallet
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"sync"
"time"
)
// New creates a new viva client for the oauth apis
func NewOAuth(clientID string, clientSecret string, demo bool) *OAuthClient {
return &OAuthClient{
Config: Config{
Demo: demo,
ClientID: clientID,
ClientSecret: clientSecret,
},
Client: httpClient,
tokenValue: &token{},
lock: &sync.RWMutex{},
}
}
func (c OAuthClient) Post(uri string, reader *bytes.Reader, v interface{}) error {
req := newRequest("POST", uri, reader)
return c.performReq(req, v)
}
func (c OAuthClient) Get(uri string, v interface{}) error {
req := newRequest("GET", uri, nil)
return c.performReq(req, v)
}
func (c OAuthClient) Patch(uri string, reader *bytes.Reader, v interface{}) error {
req := newRequest("PATCH", uri, reader)
return c.performReq(req, v)
}
func (c OAuthClient) Delete(uri string, reader *bytes.Reader, v interface{}) error {
req := newRequest("DELETE", uri, reader)
return c.performReq(req, v)
}
func (c OAuthClient) setBearerToken(req *http.Request) {
req.Header.Set("Authorization", "Bearer "+c.AuthToken())
}
func (c OAuthClient) performReq(req *http.Request, v interface{}) error {
req.Header.Add("Content-Type", "application/json")
c.setBearerToken(req)
resp, httpErr := c.Client.Do(req)
if httpErr != nil {
return fmt.Errorf("failed to perform request %s", httpErr)
}
body, bodyErr := io.ReadAll(resp.Body)
if bodyErr == nil {
resp.Body.Close()
}
if bodyErr != nil {
return bodyErr
}
if resp.StatusCode != 200 {
return fmt.Errorf("failed to perform request with status %d", resp.StatusCode)
}
return json.Unmarshal(body, v)
}
type TokenResponse struct {
AccessToken string `json:"access_token"`
ExpiresIn int64 `json:"expires_in"`
TokenType string `json:"token_type"`
Scope string `json:"scope"`
}
// Authenticate retrieves the access token to continue making requests to Viva's API. It
// returns the full response of the API and stores the token and expiration time for
// later use.
func (c OAuthClient) Authenticate() (*TokenResponse, error) {
uri := c.tokenEndpoint()
grant := []byte("grant_type=client_credentials")
req, _ := http.NewRequest("POST", uri, bytes.NewBuffer(grant))
req.SetBasicAuth(c.Config.ClientID, c.Config.ClientSecret)
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
resp, httpErr := c.Client.Do(req)
if httpErr != nil {
return nil, fmt.Errorf("failed to perform access token request %s", httpErr)
}
if resp.StatusCode != 200 {
return nil, errors.New("non successful response")
}
defer resp.Body.Close()
body, bodyErr := io.ReadAll(resp.Body)
if bodyErr != nil {
return nil, bodyErr
}
response := &TokenResponse{}
if jsonErr := json.Unmarshal(body, response); jsonErr != nil {
return nil, jsonErr
}
expiry := time.Now().Add(time.Second * time.Duration(response.ExpiresIn))
c.SetToken(response.AccessToken, expiry)
return response, nil
}
// AuthToken returns the token value
func (c OAuthClient) AuthToken() string {
c.lock.RLock()
t := c.tokenValue.value
c.lock.RUnlock()
return t
}
// SetToken sets the token value and the expiration time of the token.
func (c OAuthClient) SetToken(value string, expires time.Time) {
c.lock.Lock()
c.tokenValue.value = value
c.tokenValue.expires = expires
c.lock.Unlock()
}
// HasAuthExpired returns true if the expiry time of the token has passed and false
// otherwise.
func (c OAuthClient) HasAuthExpired() bool {
c.lock.RLock()
expires := c.tokenValue.expires
c.lock.RUnlock()
now := time.Now()
return now.After(expires)
}
func AuthBody(c Config) string {
auth := fmt.Sprintf("%s:%s", c.ClientID, c.ClientSecret)
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func BasicAuth(c Config) string {
auth := fmt.Sprintf("%s:%s", c.MerchantID, c.APIKey)
return base64.StdEncoding.EncodeToString([]byte(auth))
}
func (c OAuthClient) tokenEndpoint() string {
return fmt.Sprintf("%s/%s", c.authUri(), "/connect/token")
}
func (c OAuthClient) authUri() string {
if isDemo(c.Config) {
return "https://demo-accounts.vivapayments.com"
}
return "https://accounts.vivapayments.com"
}