-
Notifications
You must be signed in to change notification settings - Fork 6
/
introspect.go
66 lines (59 loc) · 2.03 KB
/
introspect.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
/*
* Copyright 2020 ForgeRock AS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package introspect
import (
"encoding/json"
"github.com/ForgeRock/iot-edge/internal/clock"
)
// InactiveIntrospectionBytes contains the standard introspection for an inactive token
var InactiveIntrospectionBytes = []byte(`{"active":false}`)
// IsActive returns true if the given introspection bytes indicate that the token is active
func IsActive(b []byte) bool {
introspection := struct {
Active bool `json:"active"`
}{}
if err := json.Unmarshal(b, &introspection); err != nil {
return false
}
return introspection.Active
}
// AddActive modifies the claims to indicate that the token is currently active
func AddActive(b []byte) ([]byte, error) {
var claims map[string]interface{}
err := json.Unmarshal(b, &claims)
if err != nil {
return b, err
}
claims["active"] = true
return json.Marshal(claims)
}
// ValidNow returns true if the expired and not before claims indicate that the token is valid for the current local
// time. From RFC7519:
// processing of the "exp" claim requires that the current date/time
// MUST be before the expiration date/time listed in the "exp" claim
// processing of the "nbf" claim requires that the current date/time
// MUST be after or equal to listed in the "nbf" claim
func ValidNow(b []byte) bool {
claims := struct {
Exp int64 `json:"exp"`
Nbf int64 `json:"nbf"`
}{}
if err := json.Unmarshal(b, &claims); err != nil {
return false
}
now := clock.Clock().Unix()
return now < claims.Exp && now >= claims.Nbf
}