-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
revocator_http.go
49 lines (42 loc) · 1.14 KB
/
revocator_http.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
package oauth2
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"github.com/pkg/errors"
"golang.org/x/oauth2/clientcredentials"
)
type HTTPRecovator struct {
Config *clientcredentials.Config
Dry bool
Endpoint *url.URL
Client *http.Client
}
func (r *HTTPRecovator) RevokeToken(ctx context.Context, token string) error {
var ep = *r.Endpoint
ep.Path = RevocationPath
data := url.Values{"token": []string{token}}
hreq, err := http.NewRequest("POST", ep.String(), bytes.NewBufferString(data.Encode()))
if err != nil {
return errors.WithStack(err)
}
hreq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
hreq.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
hreq.SetBasicAuth(r.Config.ClientID, r.Config.ClientSecret)
if r.Client == nil {
r.Client = http.DefaultClient
}
hres, err := r.Client.Do(hreq)
if err != nil {
return errors.WithStack(err)
}
defer hres.Body.Close()
body, _ := ioutil.ReadAll(hres.Body)
if hres.StatusCode < 200 || hres.StatusCode >= 300 {
return errors.Errorf("Expected 2xx status code but got %d.\n%s", hres.StatusCode, body)
}
return nil
}