forked from openshift/origin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapper.go
54 lines (41 loc) · 2.4 KB
/
mapper.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
package identitymapper
import (
"fmt"
userclient "github.com/openshift/client-go/user/clientset/versioned/typed/user/v1"
authapi "github.com/openshift/origin/pkg/oauthserver/api"
)
type MappingMethodType string
const (
// MappingMethodLookup does not provision a new identity or user, it only allows identities already associated with users
MappingMethodLookup MappingMethodType = "lookup"
// MappingMethodClaim associates a new identity with a user with the identity's preferred username
// if no other identities are already associated with the user
MappingMethodClaim MappingMethodType = "claim"
// MappingMethodAdd associates a new identity with a user with the identity's preferred username,
// creating the user if needed, and adding to any existing identities associated with the user
MappingMethodAdd MappingMethodType = "add"
// MappingMethodGenerate finds an available username for a new identity, based on its preferred username
// If a user with the preferred username already exists, a unique username is generated
MappingMethodGenerate MappingMethodType = "generate"
)
// NewIdentityUserMapper returns a UserIdentityMapper that does the following:
// 1. Returns an existing user if the identity exists and is associated with an existing user
// 2. Returns an error if the identity exists and is not associated with a user (or is associated with a missing user)
// 3. Handles new identities according to the requested method
func NewIdentityUserMapper(identities userclient.IdentityInterface, users userclient.UserInterface, userIdentityMapping userclient.UserIdentityMappingInterface, method MappingMethodType) (authapi.UserIdentityMapper, error) {
// initUser initializes fields in a User API object from its associated Identity
// called when adding the first Identity to a User (during create or update of a User)
initUser := NewDefaultUserInitStrategy()
switch method {
case MappingMethodLookup:
return &lookupIdentityMapper{userIdentityMapping, users}, nil
case MappingMethodClaim:
return &provisioningIdentityMapper{identities, users, NewStrategyClaim(users, initUser)}, nil
case MappingMethodAdd:
return &provisioningIdentityMapper{identities, users, NewStrategyAdd(users, initUser)}, nil
case MappingMethodGenerate:
return &provisioningIdentityMapper{identities, users, NewStrategyGenerate(users, initUser)}, nil
default:
return nil, fmt.Errorf("unsupported mapping method %q", method)
}
}