-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtoken.go
201 lines (174 loc) · 7 KB
/
token.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/*******************************************************************************
*
* Copyright 2017-2018 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, 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 gopherpolicy
import (
"fmt"
"net/http"
policy "github.com/databus23/goslo.policy"
"github.com/gophercloud/gophercloud/v2"
"github.com/gophercloud/gophercloud/v2/openstack/identity/v3/tokens"
"github.com/sapcc/go-api-declarations/cadf"
"github.com/sapcc/go-bits/internal"
)
// Enforcer contains the Enforce method that struct Token requires to check
// access permissions. This interface is satisfied by struct Enforcer from
// goslo.policy.
type Enforcer interface {
Enforce(rule string, c policy.Context) bool
}
// Token represents a validated Keystone v3 token. It is returned from
// Validator.CheckToken().
type Token struct {
// The enforcer that checks access permissions for this client token. Usually
// an instance of struct Enforcer from goslo.policy. Usually inherited from
// struct TokenValidator.
Enforcer Enforcer
// When AuthN succeeds, contains information about the client token which can
// be used to check access permissions.
Context policy.Context
// When AuthN succeeds, contains a fully-initialized ProviderClient with which
// this process can use the OpenStack API on behalf of the authenticated user.
ProviderClient *gophercloud.ProviderClient
// When AuthN fails, contains the deferred AuthN error.
Err error
// When AuthN succeeds, contains all the information needed to serialize this
// token in SerializeTokenForCache.
serializable serializableToken
}
// Require checks if the given token has the given permission according to the
// policy.json that is in effect. If not, an error response is written and false
// is returned.
func (t *Token) Require(w http.ResponseWriter, rule string) bool {
if t.Err != nil {
if t.Context.Logger != nil {
t.Context.Logger(fmt.Sprintf("returning %v because of error: %s", http.StatusUnauthorized, t.Err.Error()))
}
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return false
}
if !t.Enforcer.Enforce(rule, t.Context) {
http.Error(w, "Forbidden", http.StatusForbidden)
return false
}
return true
}
// Check is like Require, but does not write error responses.
func (t *Token) Check(rule string) bool {
return t.Err == nil && t.Enforcer.Enforce(rule, t.Context)
}
// UserUUID returns the UUID of the user for whom this token was issued, or ""
// if the token was invalid.
func (t *Token) UserUUID() string {
return t.Context.Auth["user_id"]
}
// UserName returns the name of the user for whom this token was issued, or ""
// if the token was invalid.
func (t *Token) UserName() string {
return t.Context.Auth["user_name"]
}
// UserDomainName returns the name of the domain containing the user for whom
// this token was issued, or "" if the token was invalid.
func (t *Token) UserDomainName() string {
return t.Context.Auth["user_domain_name"]
}
// UserDomainUUID returns the UUID of the domain containing the user for whom
// this token was issued, or "" if the token was invalid.
func (t *Token) UserDomainUUID() string {
return t.Context.Auth["user_domain_id"]
}
// ProjectScopeUUID returns the UUID of this token's project scope, or "" if the token is
// invalid or not scoped to a project.
func (t *Token) ProjectScopeUUID() string {
return t.Context.Auth["project_id"]
}
// ProjectScopeName returns the name of this token's project scope, or "" if the token is
// invalid or not scoped to a project.
func (t *Token) ProjectScopeName() string {
return t.Context.Auth["project_name"]
}
// ProjectScopeDomainUUID returns the UUID of this token's project scope domain, or ""
// if the token is invalid or not scoped to a project.
func (t *Token) ProjectScopeDomainUUID() string {
return t.Context.Auth["project_domain_id"]
}
// ProjectScopeDomainName returns the name of this token's project scope domain, or ""
// if the token is invalid or not scoped to a project.
func (t *Token) ProjectScopeDomainName() string {
return t.Context.Auth["project_domain_name"]
}
// DomainScopeUUID returns the UUID of this token's domain scope, or "" if the token is
// invalid or not scoped to a domain.
func (t *Token) DomainScopeUUID() string {
return t.Context.Auth["domain_id"]
}
// DomainScopeName returns the name of this token's domain scope, or "" if the token is
// invalid or not scoped to a domain.
func (t *Token) DomainScopeName() string {
return t.Context.Auth["domain_name"]
}
// ApplicationCredentialID returns the ID of the application credential that
// was used to create this token, or "" if the token was created through a
// different authentication method.
func (t *Token) ApplicationCredentialID() string {
return t.Context.Auth["application_credential_id"]
}
// AsInitiator implements the audittools.UserInfo interface.
func (t *Token) AsInitiator(host cadf.Host) cadf.Resource {
return cadf.Resource{
TypeURI: internal.StandardUserInfoTypeURI,
// information about user
Name: t.UserName(),
Domain: t.UserDomainName(),
ID: t.UserUUID(),
Host: &host,
// information about user's scope (only one of both will be filled)
DomainID: t.DomainScopeUUID(),
DomainName: t.DomainScopeName(),
ProjectID: t.ProjectScopeUUID(),
ProjectName: t.ProjectScopeName(),
ProjectDomainName: t.ProjectScopeDomainName(),
AppCredentialID: t.ApplicationCredentialID(),
}
}
////////////////////////////////////////////////////////////////////////////////
// type serializableToken
type serializableToken struct {
Token tokens.Token `json:"token_id"`
TokenData keystoneToken `json:"token_data"`
ServiceCatalog []tokens.CatalogEntry `json:"catalog"`
}
// ExtractInto implements the TokenResult interface.
func (s serializableToken) ExtractInto(value any) error {
// TokenResult.ExtractInto is only ever called with a value of type
// *keystoneToken, so this is okay
kd, ok := value.(*keystoneToken)
if !ok {
return fmt.Errorf("serializableToken.ExtractInto called with unsupported target type %T", value)
}
*kd = s.TokenData
return nil
}
// Extract implements the TokenResult interface.
func (s serializableToken) Extract() (*tokens.Token, error) {
return &s.Token, nil
}
// ExtractServiceCatalog implements the TokenResult interface.
func (s serializableToken) ExtractServiceCatalog() (*tokens.ServiceCatalog, error) {
return &tokens.ServiceCatalog{Entries: s.ServiceCatalog}, nil
}