/
header.go
99 lines (86 loc) · 2.5 KB
/
header.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
87
88
89
90
91
92
93
94
95
96
97
98
99
// Tideland Go REST Server Library - JSON Web Token - Header
//
// Copyright (C) 2016 Frank Mueller / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.
package jwt
//--------------------
// IMPORTS
//--------------------
import (
"net/http"
"strings"
"github.com/tideland/gorest/rest"
)
//--------------------
// JOB AND REQUEST HANDLING
//--------------------
// DecodeFromJob retrieves a possible JWT from
// the request inside a REST job. The JWT is only decoded.
func DecodeFromJob(job rest.Job) (JWT, error) {
return retrieveFromJob(job, nil, nil)
}
// DecodeCachedFromJob retrieves a possible JWT from the request
// inside a REST job and checks if it already is cached. The JWT is
// only decoded. In case of no error the token is added to the cache.
func DecodeCachedFromJob(job rest.Job, cache Cache) (JWT, error) {
return retrieveFromJob(job, cache, nil)
}
// VerifyFromJob retrieves a possible JWT from
// the request inside a REST job. The JWT is verified.
func VerifyFromJob(job rest.Job, key Key) (JWT, error) {
return retrieveFromJob(job, nil, key)
}
// VerifyCachedFromJob retrieves a possible JWT from the request
// inside a REST job and checks if it already is cached. The JWT is
// verified. In case of no error the token is added to the cache.
func VerifyCachedFromJob(job rest.Job, cache Cache, key Key) (JWT, error) {
return retrieveFromJob(job, cache, key)
}
// AddTokenToRequest adds a token as header to a request for
// usage by a client.
func AddTokenToRequest(req *http.Request, jwt JWT) *http.Request {
req.Header.Add("Authorization", "Bearer "+jwt.String())
return req
}
//--------------------
// PRIVATE HELPERS
//--------------------
// retrieveFromJob is the generic retrieval function with possible
// caching and verifaction.
func retrieveFromJob(job rest.Job, cache Cache, key Key) (JWT, error) {
// Retrieve token from header.
authorization := job.Request().Header.Get("Authorization")
if authorization == "" {
return nil, nil
}
fields := strings.Fields(authorization)
if len(fields) != 2 || fields[0] != "Bearer" {
return nil, nil
}
// Check cache.
if cache != nil {
jwt, ok := cache.Get(fields[1])
if ok {
return jwt, nil
}
}
// Decode or verify.
var jwt JWT
var err error
if key == nil {
jwt, err = Decode(fields[1])
} else {
jwt, err = Verify(fields[1], key)
}
if err != nil {
return nil, err
}
// Add to cache and return.
if cache != nil {
cache.Put(jwt)
}
return jwt, nil
}
// EOF