-
Notifications
You must be signed in to change notification settings - Fork 3
/
ginhydra.go
48 lines (42 loc) · 979 Bytes
/
ginhydra.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
package ginhydra
import (
hydra "github.com/ory-am/hydra/sdk"
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
)
var (
hc *hydra.Client
)
func Init(hydraClient *hydra.Client) {
hc = hydraClient
}
// TODO: Copied from fosite
func AccessTokenFromRequest(req *http.Request) string {
auth := req.Header.Get("Authorization")
split := strings.SplitN(auth, " ", 2)
if len(split) != 2 || !strings.EqualFold(split[0], "bearer") {
// Empty string returned if there's no such parameter
err := req.ParseForm()
if err != nil {
return ""
}
fmt.Println(req.Form.Get("access_token"))
return req.Form.Get("access_token")
}
return split[1]
}
func ScopesRequired(scopes ...string) gin.HandlerFunc {
return func(c *gin.Context) {
ctx, err := hc.Introspection.IntrospectToken(c, AccessTokenFromRequest(c.Request), scopes...)
if err != nil {
c.Error(err)
c.Abort()
return
}
// All required scopes are found
c.Set("hydra", ctx)
c.Next()
}
}