src.dualinventive.com/go/authentication-service/grpc/mappings.go

65 lines
1.6 KiB
Go

package grpc
import (
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"src.dualinventive.com/go/authentication-service/internal/authtokens"
"src.dualinventive.com/go/authentication-service/internal/domain"
)
func mapUser(user *domain.User) *User {
if user == nil {
return nil
}
mapped := &User{}
mapped.UserId = int64(user.ID)
mapped.UserName = user.Name
var roles = make([]*Role, 0, len(user.Roles))
for _, role := range user.Roles {
var rights []string
for _, right := range role.Rights {
rights = append(rights, right.Code)
}
roles = append(roles, &Role{
Name: role.Name,
Rights: rights,
})
}
mapped.Roles = roles
mapped.Company = &Company{
Id: int64(user.Company.ID),
Name: user.Company.Name,
Code: user.Company.Code,
}
return mapped
}
func mapTokenList(opaqueTokens map[string]string) []*OpaqueToken {
mapped := []*OpaqueToken{}
for id, userAgent := range opaqueTokens {
mapped = append(mapped, &OpaqueToken{OpaqueId: id, UserAgent: userAgent})
}
return mapped
}
func mapError(err error) error {
switch err.(type) {
case *authtokens.ErrInvalidCredentials:
return status.Error(codes.Unauthenticated, err.Error())
case *authtokens.ErrNilToken:
return status.Error(codes.Unauthenticated, err.Error())
case *authtokens.ErrTokenNotFound:
return status.Error(codes.Unauthenticated, err.Error())
case *authtokens.ErrUserNotFound:
return status.Error(codes.Unauthenticated, err.Error())
case *authtokens.ErrInvalidToken:
return status.Error(codes.Unauthenticated, err.Error())
default:
return status.Error(codes.Internal, err.Error())
}
}