-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenOp.go
86 lines (76 loc) · 1.95 KB
/
tokenOp.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
package networkutil
import (
"net/http"
"net/url"
"strings"
)
type TokenOperation struct {
CookieOp CookieOperation
}
// Create tokenOp.
//
// Arguments;
// apiUrl {string} - URL of the API you are using.
func NewTokenOp(apiUrl string) (*TokenOperation, error) {
u, err := url.Parse(apiUrl)
if err != nil {
return nil, err
}
splittedHost := strings.Split(u.String(), ".")
hostLen := len(splittedHost)
domain := strings.Join(splittedHost[hostLen-2:], ".")
cookieOp := NewCookieOp(domain)
return &TokenOperation{
CookieOp: *cookieOp,
}, nil
}
// Set the refresh token.
//
// Arguments:
// w {http.ResponseWriter} - http writer.
// refreshToken {string} - refresh token.
func (t *TokenOperation) SetRefreshToken(w http.ResponseWriter, refreshToken string) {
t.CookieOp.Set(w, "refresh_token", refreshToken, 24*30)
}
// Set the session token.
//
// Arguments:
// w {http.ResponseWriter} - http writer.
// sessionToken {string} - session token.
func (t *TokenOperation) SetSessionToken(w http.ResponseWriter, sessionToken string) {
t.CookieOp.Set(w, "session_token", sessionToken, 6)
}
// Get the refresh token.
//
// Arguments:
// r {*http.Request} - http requests.
//
// Returns:
// {string} - refresh token.
func (t *TokenOperation) GetRefreshToken(r *http.Request) (string, error) {
return t.CookieOp.Get(r, "refresh_token")
}
// Get the session token.
//
// Arguments:
// r {*http.Request} - http requests.
//
// Returns:
// {string} - session token.
func (t *TokenOperation) GetSessionToken(r *http.Request) (string, error) {
return t.CookieOp.Get(r, "session_token")
}
// Delete session and refresh tokens.
//
// Arguments:
// w {http.ResponseWriter} - http writer.
// r {*http.Request} - http requests.
func (t *TokenOperation) DeleteToken(w http.ResponseWriter, r *http.Request) error {
if err := t.CookieOp.Delete(w, r, "session_token"); err != nil {
return err
}
if err := t.CookieOp.Delete(w, r, "refresh_token"); err != nil {
return err
}
return nil
}