-
Notifications
You must be signed in to change notification settings - Fork 0
/
authorization.go
54 lines (45 loc) · 1.33 KB
/
authorization.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
package monnify
import (
"encoding/base64"
"fmt"
"net/http"
)
type (
// authorizationRes response object
authorizationRes struct {
RequestSuccessful bool `json:"requestSuccessful"`
ResponseMessage string `json:"responseMessage"`
ResponseCode string `json:"responseCode"`
ResponseBody struct {
AccessToken string `json:"accessToken"`
ExpiresIn int `json:"expiresIn"`
} `json:"responseBody"`
}
)
// basicTokenGen generates a basic token by converting into base64
func (c *Client) basicTokenGen() {
text := fmt.Sprintf("%s:%s", c.apiKey, c.secretKey)
encodedText := base64.StdEncoding.EncodeToString([]byte(text))
c.basicToken = encodedText
}
// bearerTokenGen generates a bearer token using the base64 form of the API Key and secret Key
func (c *Client) bearerTokenGen() error {
url := fmt.Sprintf("%s/v1/auth/login/", c.baseURL)
// generate basic token
c.basicTokenGen()
// set isBasic
c.isBasic = true
var response authorizationRes
if err := c.newRequest(http.MethodPost, url, nil, response); err != nil {
return err
}
c.bearerToken = response.ResponseBody.AccessToken
return nil
}
// generateNewBearerToken generates a new bearer token after the previously token has expired
func (c *Client) generateNewBearerToken() error {
if err := c.bearerTokenGen(); err != nil {
return err
}
return nil
}