-
Notifications
You must be signed in to change notification settings - Fork 1
/
introspector_http.go
64 lines (55 loc) · 1.84 KB
/
introspector_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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package oauth2
import (
"bytes"
"encoding/json"
"github.com/go-errors/errors"
"github.com/ory-am/fosite"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
type HTTPIntrospector struct {
Client *http.Client
Dry bool
Endpoint *url.URL
}
func (this *HTTPIntrospector) TokenFromRequest(r *http.Request) string {
return fosite.AccessTokenFromRequest(r)
}
func (this *HTTPIntrospector) SetClient(c *clientcredentials.Config) {
this.Client = c.Client(oauth2.NoContext)
}
// IntrospectToken is capable of introspecting tokens according to https://tools.ietf.org/html/rfc7662
//
// The HTTP API is documented at http://docs.hdyra.apiary.io/#reference/oauth2/oauth2-token-introspection
func (this *HTTPIntrospector) IntrospectToken(ctx context.Context, token string) (*Introspection, error) {
var resp = new(Introspection)
var ep = *this.Endpoint
ep.Path = IntrospectPath
data := url.Values{"token": []string{token}}
hreq, err := http.NewRequest("POST", ep.String(), bytes.NewBufferString(data.Encode()))
if err != nil {
return nil, errors.New(err)
}
hreq.Header.Add("Content-Type", "application/x-www-form-urlencoded")
hreq.Header.Add("Content-Length", strconv.Itoa(len(data.Encode())))
hres, err := this.Client.Do(hreq)
if err != nil {
return nil, errors.New(err)
}
defer hres.Body.Close()
if hres.StatusCode < 200 || hres.StatusCode >= 300 {
body, _ := ioutil.ReadAll(hres.Body)
return nil, errors.Errorf("Expected 2xx status code but got %d.\n%s", hres.StatusCode, body)
} else if err := json.NewDecoder(hres.Body).Decode(resp); err != nil {
body, _ := ioutil.ReadAll(hres.Body)
return nil, errors.Errorf("%s: %s", err, body)
} else if !resp.Active {
return nil, errors.New("Token is malformed, expired or otherwise invalid")
}
return resp, nil
}