forked from tideland/gorest
/
header.go
115 lines (99 loc) · 3.05 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Tideland GoREST - JSON Web Token - Header
//
// Copyright (C) 2016-2017 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/golib/errors"
"github.com/tideland/gorest/rest"
)
//--------------------
// REQUEST AND JOB HANDLING
//--------------------
// AddTokenToRequest adds a token as header to a request for
// usage by a client.
//
// DEPRECATED: Now AddToRequest().
func AddTokenToRequest(req *http.Request, jwt JWT) *http.Request {
return AddToRequest(req, jwt)
}
// AddToRequest adds a token as header to a request for
// usage by a client.
func AddToRequest(req *http.Request, jwt JWT) *http.Request {
req.Header.Add("Authorization", "Bearer "+jwt.String())
return req
}
// DecodeFromRequest tries to retrieve a token from a request
// header.
func DecodeFromRequest(req *http.Request) (JWT, error) {
return decodeFromRequest(req, nil, nil)
}
// 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 decodeFromRequest(job.Request(), 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 decodeFromRequest(job.Request(), 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 decodeFromRequest(job.Request(), 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 decodeFromRequest(job.Request(), cache, key)
}
//--------------------
// PRIVATE HELPERS
//--------------------
// decodeFromRequest is the generic decoder with possible
// caching and verification.
func decodeFromRequest(req *http.Request, cache Cache, key Key) (JWT, error) {
// Retrieve token from header.
authorization := req.Header.Get("Authorization")
if authorization == "" {
return nil, errors.New(ErrNoAuthorizationHeader, errorMessages)
}
fields := strings.Fields(authorization)
if len(fields) != 2 || fields[0] != "Bearer" {
return nil, errors.New(ErrInvalidAuthorizationHeader, errorMessages, authorization)
}
// 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