+8
-19
@@ -11,19 +11,8 @@ import (
|
||||
"google.golang.org/grpc/metadata"
|
||||
)
|
||||
|
||||
// Impersonate returns an authenticated reva context and the user it represents
|
||||
func Impersonate(userID *user.UserId, gwc gateway.GatewayAPIClient, machineAuthAPIKey string) (context.Context, *user.User, error) {
|
||||
usr, err := GetUser(userID, gwc, machineAuthAPIKey)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
ctx, err := ImpersonateUser(usr, gwc, machineAuthAPIKey)
|
||||
return ctx, usr, err
|
||||
}
|
||||
|
||||
// GetUser gets the specified user
|
||||
func GetUser(userID *user.UserId, gwc gateway.GatewayAPIClient, machineAuthAPIKey string) (*user.User, error) {
|
||||
func GetUser(userID *user.UserId, gwc gateway.GatewayAPIClient) (*user.User, error) {
|
||||
getUserResponse, err := gwc.GetUser(context.Background(), &user.GetUserRequest{UserId: userID})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -35,19 +24,19 @@ func GetUser(userID *user.UserId, gwc gateway.GatewayAPIClient, machineAuthAPIKe
|
||||
return getUserResponse.GetUser(), nil
|
||||
}
|
||||
|
||||
// ImpersonateUser impersonates the given user
|
||||
func ImpersonateUser(usr *user.User, gwc gateway.GatewayAPIClient, machineAuthAPIKey string) (context.Context, error) {
|
||||
ctx := revactx.ContextSetUser(context.Background(), usr)
|
||||
// GetServiceUserContext returns an authenticated context of the given service user
|
||||
func GetServiceUserContext(serviceUserID string, gwc gateway.GatewayAPIClient, serviceUserSecret string) (context.Context, error) {
|
||||
ctx := context.Background()
|
||||
authRes, err := gwc.Authenticate(ctx, &gateway.AuthenticateRequest{
|
||||
Type: "machine",
|
||||
ClientId: "userid:" + usr.GetId().GetOpaqueId(),
|
||||
ClientSecret: machineAuthAPIKey,
|
||||
Type: "serviceaccounts",
|
||||
ClientId: serviceUserID,
|
||||
ClientSecret: serviceUserSecret,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if authRes.GetStatus().GetCode() != rpc.Code_CODE_OK {
|
||||
return nil, fmt.Errorf("error impersonating user: %s", authRes.Status.Message)
|
||||
return nil, fmt.Errorf("error authenticating service user: %s", authRes.Status.Message)
|
||||
}
|
||||
|
||||
return metadata.AppendToOutgoingContext(ctx, revactx.TokenHeader, authRes.Token), nil
|
||||
|
||||
Reference in New Issue
Block a user