-
Notifications
You must be signed in to change notification settings - Fork 280
/
claims_scope.go
81 lines (70 loc) · 2.09 KB
/
claims_scope.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
package oidc
import (
"context"
"github.com/hashicorp/boundary/internal/auth/oidc/store"
"github.com/hashicorp/boundary/internal/errors"
"google.golang.org/protobuf/proto"
)
const (
// defaultClaimsScopeTableName defines the default table name for an ClaimsScope
defaultClaimsScopeTableName = "auth_oidc_scope"
DefaultClaimsScope = "openid"
)
// ClaimsScope defines optional OIDC scope values that are used to request
// claims, in addition to the default scope of "openid" (see: DefaultClaimsScope).
//
// see: https://openid.net/specs/openid-connect-core-1_0.html#ScopeClaims
type ClaimsScope struct {
*store.ClaimsScope
tableName string
}
func NewClaimsScope(ctx context.Context, authMethodId, claimsScope string) (*ClaimsScope, error) {
const op = "oidc.NewClaimsScope"
cs := &ClaimsScope{
ClaimsScope: &store.ClaimsScope{
OidcMethodId: authMethodId,
Scope: claimsScope,
},
}
if err := cs.validate(ctx, op); err != nil {
return nil, err
}
return cs, nil
}
// validate the ClaimsScope. On success, it will return nil.
func (cs *ClaimsScope) validate(ctx context.Context, caller errors.Op) error {
if cs.OidcMethodId == "" {
return errors.New(ctx, errors.InvalidParameter, caller, "missing oidc auth method id")
}
if cs.Scope == "" {
return errors.New(ctx, errors.InvalidParameter, caller, "missing claims scope")
}
if cs.Scope == DefaultClaimsScope {
return errors.New(ctx, errors.InvalidParameter, caller, "openid is the default scope and cannot be added as optional")
}
return nil
}
// AllocClaimsScope makes an empty one in memory
func AllocClaimsScope() ClaimsScope {
return ClaimsScope{
ClaimsScope: &store.ClaimsScope{},
}
}
// Clone a ClaimsScope
func (cs *ClaimsScope) Clone() *ClaimsScope {
cp := proto.Clone(cs.ClaimsScope)
return &ClaimsScope{
ClaimsScope: cp.(*store.ClaimsScope),
}
}
// TableName returns the table name.
func (s *ClaimsScope) TableName() string {
if s.tableName != "" {
return s.tableName
}
return defaultClaimsScopeTableName
}
// SetTableName sets the table name.
func (s *ClaimsScope) SetTableName(n string) {
s.tableName = n
}