Signed-off-by: Nikolai Rodionov <allanger@badhouseplants.net>
This commit is contained in:
@@ -2,6 +2,7 @@ package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"gitea.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
||||
tokens "gitea.badhouseplants.net/softplayer/softplayer-go-proto/pkg/tokens/v1"
|
||||
@@ -37,23 +38,32 @@ func (srv *TokensServer) CreateToken(ctx context.Context, in *tokens.CreateToken
|
||||
if claims.UserID == "" {
|
||||
return nil, status.Error(codes.Aborted, "Context is invalid")
|
||||
}
|
||||
|
||||
if in.TokenPermissions == nil {
|
||||
return nil, status.Error(codes.InvalidArgument, "Permissions must be set")
|
||||
}
|
||||
permissions := map[string][]string{}
|
||||
for service, methods := range in.TokenPermissions.Permissions {
|
||||
permissions[service] = methods.GetMethods()
|
||||
}
|
||||
|
||||
tokenData := &controllers.TokenData{
|
||||
Name: in.TokenMetadata.GetName(),
|
||||
UserID: claims.UserID,
|
||||
Scopes: &controllers.Scopes{},
|
||||
Scopes: permissions,
|
||||
}
|
||||
|
||||
token, err := srv.tokenCtrl.Create(ctx, tokenData)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if errors.Is(err, controllers.ErrServerError) {
|
||||
return nil, status.Error(codes.Internal, "Something is broken on our side")
|
||||
}
|
||||
return nil, status.Error(codes.Aborted, "Couldn't create a token")
|
||||
}
|
||||
|
||||
return &tokens.CreateTokenResponse{
|
||||
TokenUuid: &tokens.TokenUUID{},
|
||||
TokenMetadata: &tokens.TokenMetadata{},
|
||||
TokenPermissions: &tokens.TokenPermissions{},
|
||||
TokenValue: &tokens.TokenValue{Token: token},
|
||||
}, status.Error(codes.Unimplemented, "Method is not implemented")
|
||||
TokenValue: &tokens.TokenValue{Token: token},
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ForceTokenExpiration implements [v1.TokensServiceServer].
|
||||
@@ -86,8 +96,8 @@ func (srv *TokensServer) ListPermissions(in *emptypb.Empty, stream grpc.ServerSt
|
||||
data := srv.tokenCtrl.ListPermissions(stream.Context())
|
||||
for key, data := range data {
|
||||
result := &tokens.ListPermissionsResponse{
|
||||
Permissions: &tokens.Permissions{
|
||||
AvailabiePermissions: map[string]*tokens.MethodList{
|
||||
Permissions: &tokens.TokenPermissions{
|
||||
Permissions: map[string]*tokens.MethodList{
|
||||
key: {Methods: data},
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user