forked from Strubbl/wallabago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.go
67 lines (61 loc) · 1.81 KB
/
token.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
package wallabago
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var token Token
// Token represents the object being returned from the oauth process at the API containing the access token, expire time, type of token, scope and a refresh token
type Token struct {
AccessToken string `json:"access_token"`
ExpiresIn int `json:"expires_in"`
TokenType string `json:"token_type"`
Scope string
RefreshToken string `json:"refresh_token"`
}
// getToken will use the credentials set in the configuration to
// request an access token from the wallabag API
func getToken() (Token, error) {
var token Token
tokenURL := Config.WallabagURL + "/oauth/v2/token"
resp, err := http.PostForm(tokenURL,
url.Values{"grant_type": {"password"},
"client_id": {Config.ClientID},
"client_secret": {Config.ClientSecret},
"username": {Config.UserName},
"password": {Config.UserPassword},
})
if err != nil {
return token, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return token, fmt.Errorf("getToken: bad response from server: %v", resp.StatusCode)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return token, err
}
//log.Printf("GetToken: body=%v\n", string(body))
err = json.Unmarshal(body, &token)
return token, err
}
func checkForToken() error {
var err error
if token.TokenType == "" || token.AccessToken == "" {
token, err = getToken()
}
return err
}
// GetAuthTokenHeader will make sure there's a working token and
// return a valid string to be used as an Authentication: header
func GetAuthTokenHeader() string {
checkForToken()
if token.TokenType == "" || token.AccessToken == "" {
return ""
}
return strings.ToUpper(string(token.TokenType[0])) + token.TokenType[1:len(token.TokenType)] + " " + token.AccessToken
}