-
Notifications
You must be signed in to change notification settings - Fork 1
/
revoke_token.go
61 lines (52 loc) · 1.53 KB
/
revoke_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
package authorizer
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
// RevokeTokenInput defines attributes for /oauth/revoke request
type RevokeTokenInput struct {
RefreshToken string `json:"refresh_token"`
}
// RevokeToken is method attached to AuthorizerClient.
// It performs /oauth/revoke api call on authorizer instance.
// It takes RevokeTokenInput reference as parameter and returns Response reference or error.
// For implementation details check RevokeTokenExample examples/revoke_token.go
func (c *AuthorizerClient) RevokeToken(req *RevokeTokenInput) (*Response, error) {
if req.RefreshToken == "" {
return nil, errors.New("refresh_token is required")
}
// Marshal it into JSON prior to requesting
jsonReq, err := json.Marshal(map[string]string{
"refresh_token": req.RefreshToken,
"client_id": c.ClientID,
})
if err != nil {
return nil, err
}
client := http.Client{}
httpReq, err := http.NewRequest(http.MethodPost, c.AuthorizerURL+"/oauth/revoke", bytes.NewReader(jsonReq))
if err != nil {
return nil, err
}
// set the default extra headers
for key, val := range c.ExtraHeaders {
httpReq.Header.Add(key, val)
}
httpRes, err := client.Do(httpReq)
if err != nil {
return nil, err
}
// Need to close the response stream, once response is read.
// Hence defer close. It will automatically take care of it.
defer httpRes.Body.Close()
bodyBytes, err := ioutil.ReadAll(httpRes.Body)
if err != nil {
return nil, err
}
var res *Response
json.Unmarshal(bodyBytes, &res)
return res, nil
}