forked from hashicorp/vault
-
Notifications
You must be signed in to change notification settings - Fork 0
/
path_issue.go
153 lines (135 loc) · 3.72 KB
/
path_issue.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package jwt
import (
"encoding/json"
"fmt"
"time"
jwt "github.com/dgrijalva/jwt-go"
"github.com/hashicorp/vault/helper/uuid"
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
)
func pathIssue(b *backend) *framework.Path {
return &framework.Path{
Pattern: "issue/" + framework.GenericNameRegex("role"),
Fields: map[string]*framework.FieldSchema{
"role": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The desired role with configuration for this request",
},
"issuer": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The issuer of the token",
},
"subject": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The subject of the token",
},
"audience": &framework.FieldSchema{
Type: framework.TypeString,
Description: "The audience of the token",
},
"expiration": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "This will define the expiration in NumericDate value",
},
"not_before": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "Defines the time before which the JWT MUST NOT be accepted for processing",
},
"issued_at": &framework.FieldSchema{
Type: framework.TypeInt,
Description: "The time the JWT was issued",
},
"jti": &framework.FieldSchema{
Type: framework.TypeString,
Description: "Unique identifier for the JWT",
},
"claims": &framework.FieldSchema{
Type: framework.TypeString,
Description: "JSON Object of Claims for the JWT Token",
},
},
Callbacks: map[logical.Operation]framework.OperationFunc{
logical.WriteOperation: b.pathIssueWrite,
},
}
}
func (b *backend) pathIssueWrite(
req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
roleName := data.Get("role").(string)
// Get the role
role, err := b.getRole(req.Storage, roleName)
if err != nil {
return nil, err
}
if role == nil {
return logical.ErrorResponse(fmt.Sprintf("Unknown role: %s", roleName)), nil
}
claims := map[string]interface{}{
"initial": "ok",
}
if role.Issuer != "" {
claims["iss"] = role.Issuer
}
if role.Subject != "" {
claims["sub"] = role.Subject
}
if role.Audience != "" {
claims["aud"] = role.Audience
}
if data.Get("not_before") == 0 {
claims["nbf"] = int(time.Now().Unix())
}
if data.Get("issued_at") == 0 {
claims["iat"] = int(time.Now().Unix())
}
if data.Get("jti") == "" {
claims["jti"] = uuid.GenerateUUID()
}
if data.Get("issuer") != "" {
claims["iss"] = data.Get("issuer").(string)
}
if data.Get("subject") != "" {
claims["sub"] = data.Get("subject").(string)
}
if data.Get("audience") != "" {
claims["aud"] = data.Get("audience").(string)
}
if data.Get("expiration").(int) > 0 {
claims["exp"] = data.Get("expiration").(int)
}
if data.Get("not_before").(int) > 0 {
claims["nbf"] = data.Get("not_before").(int)
}
if data.Get("issued_at").(int) > 0 {
claims["iat"] = data.Get("issued_at").(int)
}
if data.Get("jti") != "" {
claims["jti"] = data.Get("jti").(string)
}
if data.Get("claims").(string) != "" {
// Parse JSON using unmarshal
var uc map[string]interface{}
err := json.Unmarshal([]byte(data.Get("claims").(string)), &uc)
if err != nil {
return nil, err
}
for k, v := range uc {
claims[k] = v
}
}
delete(claims, "initial")
token := jwt.New(jwt.GetSigningMethod(role.Algorithm))
token.Claims = claims
tokenString, err := token.SignedString([]byte(role.Key))
if err != nil {
return nil, err
}
resp := &logical.Response{
Data: map[string]interface{}{
"jti": claims["jti"].(string),
"token": tokenString,
},
}
return resp, nil
}