-
Notifications
You must be signed in to change notification settings - Fork 414
/
builtin.go
184 lines (155 loc) · 6.27 KB
/
builtin.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
package builtin
import (
"context"
"time"
"github.com/ovh/cds/engine/api/authentication"
"github.com/ovh/cds/engine/gorpmapper"
"github.com/ovh/cds/sdk"
"github.com/rockbears/log"
)
var _ sdk.Driver = new(BuiltinDriver)
// NewBuiltinDriver returns a new initialized driver for builtin authentication.
func NewBuiltinDriver() sdk.Driver {
return &BuiltinDriver{}
}
type BuiltinDriver struct{}
func (d BuiltinDriver) GetUserInfoFromDriver(ctx context.Context, req sdk.AuthConsumerSigninRequest) (sdk.AuthDriverUserInfo, error) {
var userInfo sdk.AuthDriverUserInfo
token, err := req.StringE("token")
if err != nil {
return userInfo, sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing or invalid authentication token")
}
consumerID, _, err := CheckSigninConsumerToken(token)
if err != nil {
return userInfo, err
}
log.Debug(ctx, "builtin.GetUserInfo> %s", consumerID)
return sdk.AuthDriverUserInfo{
ExternalID: consumerID,
}, nil
}
// CheckSigninRequest checks that given driver request is valid for a signin with auth builtin.
func (d BuiltinDriver) CheckSigninRequest(req sdk.AuthConsumerSigninRequest) error {
token, err := req.StringE("token")
if err != nil {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "missing or invalid authentication token")
}
_, _, err = CheckSigninConsumerToken(token)
return err
}
// NewConsumer returns a new builtin consumer for given data.
// The parent consumer should be given with all data loaded including the authentified user.
type NewConsumerOptions struct {
Name string
Description string
Duration time.Duration
GroupIDs []int64
Scopes sdk.AuthConsumerScopeDetails
ServiceName *string
ServiceType *string
ServiceRegion *string
ServiceIgnoreJobWithNoRegion *bool
}
func NewConsumer(ctx context.Context, db gorpmapper.SqlExecutorWithTx, opts NewConsumerOptions, parentConsumer *sdk.AuthUserConsumer) (*sdk.AuthUserConsumer, string, error) {
if opts.Name == "" {
return nil, "", sdk.NewErrorFrom(sdk.ErrWrongRequest, "name should be given to create a built in consumer")
}
// For each given group id check if it's in parent consumer group ids.
// When the parent is a builtin consumer even if it was created by an admin we should check groups to prevent
// creating child with more permission than parents.
if parentConsumer.Type == sdk.ConsumerBuiltin || !parentConsumer.Admin() {
// Only if parentGroupIDs aren't empty. Because empty means all groups access
if len(parentConsumer.AuthConsumerUser.GroupIDs) > 0 {
parentGroupIDs := parentConsumer.GetGroupIDs()
for i := range opts.GroupIDs {
if !sdk.IsInInt64Array(opts.GroupIDs[i], parentGroupIDs) {
return nil, "", sdk.WrapError(sdk.ErrWrongRequest, "invalid given group id %d", opts.GroupIDs[i])
}
}
}
}
// Check that given scopes are valid and if they match parent scopes
parentScope := parentConsumer.AuthConsumerUser.ScopeDetails
if err := checkNewConsumerScopes(parentScope, opts.Scopes); err != nil {
return nil, "", err
}
c := sdk.AuthUserConsumer{
AuthConsumer: sdk.AuthConsumer{
Name: opts.Name,
Description: opts.Description,
ParentID: &parentConsumer.ID,
Type: sdk.ConsumerBuiltin,
ValidityPeriods: sdk.NewAuthConsumerValidityPeriod(time.Now(), opts.Duration),
},
AuthConsumerUser: sdk.AuthUserConsumerData{
AuthentifiedUserID: parentConsumer.AuthConsumerUser.AuthentifiedUserID,
Data: map[string]string{},
GroupIDs: opts.GroupIDs,
ScopeDetails: opts.Scopes,
ServiceName: opts.ServiceName,
ServiceType: opts.ServiceType,
ServiceRegion: opts.ServiceRegion,
ServiceIgnoreJobWithNoRegion: opts.ServiceIgnoreJobWithNoRegion,
},
}
if err := authentication.InsertUserConsumer(ctx, db, &c); err != nil {
return nil, "", err
}
jws, err := NewSigninConsumerToken(&c)
if err != nil {
return nil, "", err
}
return &c, jws, nil
}
func checkNewConsumerScopes(parentScopes, scopes sdk.AuthConsumerScopeDetails) error {
// At least one scope should be given, for each given scope checks if its authorized and if it's in parent scopes
if len(scopes) == 0 {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "built in consumer creation requires at least one scope to be set")
}
if err := scopes.IsValid(); err != nil {
return err
}
// If parent scopes length equals 0 this means all scopes else checks that given scope is in parent scopes
if len(parentScopes) == 0 {
return nil
}
for i := range scopes {
var validScope bool
for j := range parentScopes {
if scopes[i].Scope == parentScopes[j].Scope {
// if no endpoint restrictions on parent scope is valid
if len(parentScopes[j].Endpoints) == 0 {
validScope = true
break
}
// invalid if no endpoint given and parents endpoint list is not empty
if len(scopes[i].Endpoints) == 0 {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid endpoints for scope %s when creating built in consumer", scopes[i].Scope)
}
// if parent as scope restrictions, child should contains only all or a subset of those restrictions
for _, e := range scopes[i].Endpoints {
existsParentEndpoint, parentEndpoint := parentScopes[j].Endpoints.FindEndpoint(e.Route)
if !existsParentEndpoint {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid given route %s for scope %s when creating built in consumer", e.Route, scopes[i].Scope)
}
if len(parentEndpoint.Methods) > 0 {
if len(e.Methods) == 0 {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid given methods for route %s and scope %s when creating built in consumer", e.Route, scopes[i].Scope)
}
for _, m := range e.Methods {
if !parentEndpoint.Methods.Contains(m) {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid given method %s for route %s and scope %s when creating built in consumer", m, e.Route, scopes[i].Scope)
}
}
}
}
validScope = true
break
}
}
if !validScope {
return sdk.NewErrorFrom(sdk.ErrWrongRequest, "invalid given scope %s when creating built in consumer", scopes[i])
}
}
return nil
}