github.com/cs3org/reva/v2@v2.27.7/pkg/ocm/user/user.go (about) 1 package user 2 3 import ( 4 "encoding/base64" 5 "fmt" 6 "strings" 7 8 userpb "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 9 ) 10 11 // FederatedID creates a federated user id by 12 // 1. stripping the protocol from the domain and 13 // 2. base64 encoding the opaque id with the domain to get a unique identifier that cannot collide with other users 14 func FederatedID(id *userpb.UserId, domain string) *userpb.UserId { 15 opaqueId := base64.URLEncoding.EncodeToString([]byte(id.OpaqueId + "@" + id.Idp)) 16 return &userpb.UserId{ 17 Type: userpb.UserType_USER_TYPE_FEDERATED, 18 Idp: domain, 19 OpaqueId: opaqueId, 20 } 21 } 22 23 // RemoteID creates a remote user id by 24 // 1. decoding the base64 encoded opaque id 25 // 2. splitting the opaque id at the last @ to get the opaque id and the domain 26 func RemoteID(id *userpb.UserId) *userpb.UserId { 27 remoteId := &userpb.UserId{ 28 Type: userpb.UserType_USER_TYPE_PRIMARY, 29 Idp: id.Idp, 30 OpaqueId: id.OpaqueId, 31 } 32 bytes, err := base64.URLEncoding.DecodeString(id.GetOpaqueId()) 33 if err != nil { 34 return remoteId 35 } 36 remote := string(bytes) 37 last := strings.LastIndex(remote, "@") 38 if last == -1 { 39 return remoteId 40 } 41 remoteId.OpaqueId = remote[:last] 42 remoteId.Idp = remote[last+1:] 43 44 return remoteId 45 } 46 47 // FormatOCMUser formats a user id in the form of <opaque-id>@<idp> used by the OCM API in shareWith, owner and creator fields 48 func FormatOCMUser(u *userpb.UserId) string { 49 return fmt.Sprintf("%s@%s", u.OpaqueId, u.Idp) 50 }