v2: fixed oauth errors, oauth clients native control added
This commit is contained in:
@@ -43,18 +43,6 @@ func (s *Service) Register(ctx context.Context, input models.UserRegisterInput)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
count, err := s.users.CountUsers(ctx)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
if count == 1 {
|
||||
position, err := s.positions.GetPositionByCode(ctx, "kotosoft_id_staff")
|
||||
if err == nil {
|
||||
now := time.Now()
|
||||
_ = s.positions.AssignPosition(ctx, user.ID, position.ID, nil, now, nil)
|
||||
}
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -1,86 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/dusiburg/kotosoft-id/internal/repositories"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CitizenshipService struct {
|
||||
r *repositories.CitizenshipRepository
|
||||
}
|
||||
|
||||
func NewCitizenshipService(r *repositories.CitizenshipRepository) *CitizenshipService {
|
||||
return &CitizenshipService{r: r}
|
||||
}
|
||||
|
||||
func (s *CitizenshipService) GetCitizenshipApplicationByUserId(ctx context.Context, userID uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
application, err := s.r.GetApplicationByUserId(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
if application == nil {
|
||||
return nil, models.ErrNotFound
|
||||
}
|
||||
|
||||
return application, nil
|
||||
}
|
||||
|
||||
func (s *CitizenshipService) CreateCitizenshipApplication(ctx context.Context, userID, regionID uuid.UUID) (*models.CitizenshipApplication, error) {
|
||||
exists, err := s.r.GetApplicationByUserId(ctx, userID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
if exists != nil {
|
||||
return nil, models.ErrAlreadyExists
|
||||
}
|
||||
|
||||
application, err := s.r.CreateApplication(ctx, models.CitizenshipApplication{
|
||||
UserId: userID,
|
||||
Status: models.StatusPending,
|
||||
RegionId: regionID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
return application, nil
|
||||
}
|
||||
|
||||
func (s *CitizenshipService) ReviewCitizenshipApplication(ctx context.Context, applicationID, reviewerID uuid.UUID, input models.ReviewCitizenshipApplicationInput) (*models.CitizenshipApplication, error) {
|
||||
if input.Status != models.StatusApproved && input.Status != models.StatusRejected {
|
||||
return nil, models.ErrBadRequest
|
||||
}
|
||||
if input.Status == models.StatusRejected && (input.RejectReason == nil || *input.RejectReason == "") {
|
||||
return nil, models.ErrBadRequest.WithMessage("Reject reason is required")
|
||||
}
|
||||
|
||||
application, err := s.r.GetApplicationById(ctx, applicationID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
if application == nil {
|
||||
return nil, models.ErrNotFound
|
||||
}
|
||||
if application.Status != models.StatusPending {
|
||||
return nil, models.ErrBadRequest.WithMessage("Application is not pending")
|
||||
}
|
||||
|
||||
rejectReason := input.RejectReason
|
||||
if input.Status == models.StatusApproved {
|
||||
rejectReason = nil
|
||||
}
|
||||
|
||||
if err := s.r.UpdateApplication(ctx, applicationID, input.Status, reviewerID, rejectReason); err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
updated, err := s.r.GetApplicationById(ctx, applicationID)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
|
||||
return updated, nil
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) CreateClient(ctx context.Context, input models.ClientCreateInput) (*models.OAuthClient, string, error) {
|
||||
clientID, err := newOpaqueToken(12)
|
||||
if err != nil {
|
||||
return nil, "", models.ErrInternal
|
||||
}
|
||||
rawSecret, err := newOpaqueToken(32)
|
||||
if err != nil {
|
||||
return nil, "", models.ErrInternal
|
||||
}
|
||||
hashedSecret, err := HashClientSecret(rawSecret)
|
||||
if err != nil {
|
||||
return nil, "", models.ErrInternal
|
||||
}
|
||||
|
||||
redirectURIsJSON, _ := json.Marshal(input.RedirectURIs)
|
||||
grantTypesJSON, _ := json.Marshal(input.GrantTypes)
|
||||
scopesJSON, _ := json.Marshal(input.Scopes)
|
||||
|
||||
client, err := s.oauth.CreateOAuthClient(ctx, models.OAuthClient{
|
||||
ClientID: clientID,
|
||||
ClientSecret: hashedSecret,
|
||||
Name: input.Name,
|
||||
RedirectURIs: redirectURIsJSON,
|
||||
GrantTypes: grantTypesJSON,
|
||||
Scopes: scopesJSON,
|
||||
Trusted: false,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, "", models.ErrInternal
|
||||
}
|
||||
|
||||
return client, rawSecret, nil
|
||||
}
|
||||
|
||||
func (s *Service) ListClients(ctx context.Context) ([]models.OAuthClient, error) {
|
||||
return s.oauth.ListOAuthClients(ctx)
|
||||
}
|
||||
|
||||
func (s *Service) GetClient(ctx context.Context, id uuid.UUID) (*models.OAuthClient, error) {
|
||||
client, err := s.oauth.GetOAuthClientByIDAdmin(ctx, id)
|
||||
if err != nil {
|
||||
return nil, models.ErrNotFound
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (s *Service) UpdateClient(ctx context.Context, id uuid.UUID, input models.ClientUpdateInput) (*models.OAuthClient, error) {
|
||||
client, err := s.oauth.GetOAuthClientByIDAdmin(ctx, id)
|
||||
if err != nil {
|
||||
return nil, models.ErrNotFound
|
||||
}
|
||||
|
||||
if input.Name != nil {
|
||||
client.Name = *input.Name
|
||||
}
|
||||
if input.RedirectURIs != nil {
|
||||
b, _ := json.Marshal(*input.RedirectURIs)
|
||||
client.RedirectURIs = b
|
||||
}
|
||||
if input.GrantTypes != nil {
|
||||
b, _ := json.Marshal(*input.GrantTypes)
|
||||
client.GrantTypes = b
|
||||
}
|
||||
if input.Scopes != nil {
|
||||
b, _ := json.Marshal(*input.Scopes)
|
||||
client.Scopes = b
|
||||
}
|
||||
if input.Trusted != nil {
|
||||
client.Trusted = *input.Trusted
|
||||
}
|
||||
if input.IsActive != nil {
|
||||
client.IsActive = *input.IsActive
|
||||
}
|
||||
|
||||
updated, err := s.oauth.UpdateOAuthClient(ctx, *client)
|
||||
if err != nil {
|
||||
return nil, models.ErrInternal
|
||||
}
|
||||
return updated, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeactivateClient(ctx context.Context, id uuid.UUID) error {
|
||||
if err := s.oauth.DeactivateOAuthClient(ctx, id); err != nil {
|
||||
return models.ErrInternal
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -1,174 +0,0 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/dusiburg/kotosoft-id/internal/models"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func (s *Service) GetAllPermissions(ctx context.Context) ([]models.Permission, error) {
|
||||
return s.positions.GetAllPermissions(ctx, "en")
|
||||
}
|
||||
|
||||
func (s *Service) AddPermissionToPosition(ctx context.Context, positionCode, permissionCode string) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, positionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
permission, err := s.positions.GetPermissionByCode(ctx, permissionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
return s.positions.AddPermissionToPosition(ctx, position.ID, permission.ID)
|
||||
}
|
||||
|
||||
func (s *Service) RemovePermissionFromPosition(ctx context.Context, positionCode, permissionCode string) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, positionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
permission, err := s.positions.GetPermissionByCode(ctx, permissionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
return s.positions.RemovePermissionFromPosition(ctx, position.ID, permission.ID)
|
||||
}
|
||||
|
||||
func (s *Service) GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error) {
|
||||
if locale == "" {
|
||||
locale = "en"
|
||||
}
|
||||
return s.positions.GetUserPositions(ctx, userID, locale)
|
||||
}
|
||||
|
||||
func (s *Service) GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error) {
|
||||
if locale == "" {
|
||||
locale = "en"
|
||||
}
|
||||
return s.positions.GetAllPositions(ctx, locale)
|
||||
}
|
||||
|
||||
func (s *Service) GetUsersWithPositions(ctx context.Context, locale string) ([]models.UserWithPositions, error) {
|
||||
if locale == "" {
|
||||
locale = "en"
|
||||
}
|
||||
return s.positions.GetUsersWithPositions(ctx, locale)
|
||||
}
|
||||
|
||||
func (s *Service) UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error {
|
||||
if locale == "" {
|
||||
locale = "en"
|
||||
}
|
||||
return s.positions.UpsertPositionForLocale(ctx, code, locale, name, description)
|
||||
}
|
||||
|
||||
func (s *Service) DeletePosition(ctx context.Context, code string) error {
|
||||
return s.positions.DeletePositionByCode(ctx, code)
|
||||
}
|
||||
|
||||
func (s *Service) AssignPosition(ctx context.Context, adminID, targetUserID uuid.UUID, input models.AssignPositionInput) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, input.PositionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
validFrom := input.ValidFrom
|
||||
if validFrom.IsZero() {
|
||||
validFrom = time.Now()
|
||||
}
|
||||
|
||||
return s.positions.AssignPosition(ctx, targetUserID, position.ID, &adminID, validFrom, input.ValidTo)
|
||||
}
|
||||
|
||||
func (s *Service) RevokePosition(ctx context.Context, targetUserID uuid.UUID, positionCode string) error {
|
||||
position, err := s.positions.GetPositionByCode(ctx, positionCode)
|
||||
if err != nil {
|
||||
return models.ErrNotFound
|
||||
}
|
||||
|
||||
if position.Code == "kotosoft_id_staff" {
|
||||
return models.ErrForbidden
|
||||
}
|
||||
|
||||
return s.positions.RevokePosition(ctx, targetUserID, position.ID)
|
||||
}
|
||||
|
||||
func (s *Service) GetAllPermissionsForLocale(ctx context.Context, locale string) ([]models.Permission, error) {
|
||||
if locale == "" {
|
||||
locale = "en"
|
||||
}
|
||||
return s.positions.GetAllPermissions(ctx, locale)
|
||||
}
|
||||
|
||||
func (s *Service) CreatePermission(ctx context.Context, code, locale string, description *string) error {
|
||||
if locale == "" {
|
||||
locale = "en"
|
||||
}
|
||||
return s.positions.CreatePermission(ctx, code, locale, description)
|
||||
}
|
||||
|
||||
func (s *Service) UpdatePermission(ctx context.Context, code, locale string, description *string) error {
|
||||
if locale == "" {
|
||||
locale = "en"
|
||||
}
|
||||
return s.positions.UpdatePermission(ctx, code, locale, description)
|
||||
}
|
||||
|
||||
func (s *Service) DeletePermission(ctx context.Context, code string) error {
|
||||
return s.positions.DeletePermission(ctx, code)
|
||||
}
|
||||
|
||||
func (s *Service) UserHasPermission(ctx context.Context, userID uuid.UUID, permissionCode string) (bool, error) {
|
||||
allowed, _, err := s.positions.UserHasPermission(ctx, userID, permissionCode)
|
||||
return allowed, err
|
||||
}
|
||||
|
||||
type IntrospectResponse struct {
|
||||
Active bool `json:"active"`
|
||||
Sub string `json:"sub"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
Positions []models.PositionLocalized `json:"positions"`
|
||||
Permissions []models.Permission `json:"permissions"`
|
||||
}
|
||||
|
||||
func (s *Service) IntrospectToken(ctx context.Context, token string) (*IntrospectResponse, error) {
|
||||
info, err := s.ValidateAccessToken(ctx, token)
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
|
||||
user, err := s.users.GetUserByID(ctx, info.UserID)
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
|
||||
positions, err := s.positions.GetUserPositions(ctx, user.ID, "en")
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
if positions == nil {
|
||||
positions = []models.PositionLocalized{}
|
||||
}
|
||||
|
||||
allPerms, err := s.positions.GetUserPermissions(ctx, user.ID, "en")
|
||||
if err != nil {
|
||||
return &IntrospectResponse{Active: false}, nil
|
||||
}
|
||||
if allPerms == nil {
|
||||
allPerms = []models.Permission{}
|
||||
}
|
||||
|
||||
return &IntrospectResponse{
|
||||
Active: true,
|
||||
Sub: user.ID.String(),
|
||||
IsAdmin: user.IsAdmin,
|
||||
Positions: positions,
|
||||
Permissions: allPerms,
|
||||
}, nil
|
||||
}
|
||||
@@ -32,6 +32,11 @@ type UserRepo interface {
|
||||
type OAuthRepo interface {
|
||||
GetOAuthClientByClientID(ctx context.Context, clientID string) (*models.OAuthClient, error)
|
||||
GetOAuthClientByID(ctx context.Context, id uuid.UUID) (*models.OAuthClient, error)
|
||||
CreateOAuthClient(ctx context.Context, client models.OAuthClient) (*models.OAuthClient, error)
|
||||
ListOAuthClients(ctx context.Context) ([]models.OAuthClient, error)
|
||||
GetOAuthClientByIDAdmin(ctx context.Context, id uuid.UUID) (*models.OAuthClient, error)
|
||||
UpdateOAuthClient(ctx context.Context, client models.OAuthClient) (*models.OAuthClient, error)
|
||||
DeactivateOAuthClient(ctx context.Context, id uuid.UUID) error
|
||||
CreateAuthorizationCode(ctx context.Context, row models.AuthorizationCodeRow) (*models.AuthorizationCodeRow, error)
|
||||
GetAuthorizationCodeByHash(ctx context.Context, codeHash string) (*models.AuthorizationCodeRow, error)
|
||||
ConsumeAuthorizationCodeByID(ctx context.Context, id uuid.UUID) error
|
||||
@@ -47,31 +52,6 @@ type OAuthRepo interface {
|
||||
UpsertClient(ctx context.Context, client models.OAuthClient) error
|
||||
}
|
||||
|
||||
type PositionRepo interface {
|
||||
GetAllPermissions(ctx context.Context, locale string) ([]models.Permission, error)
|
||||
GetPermissionByCode(ctx context.Context, code string) (*models.Permission, error)
|
||||
GetPositionPermissions(ctx context.Context, positionID uuid.UUID, locale string) ([]models.Permission, error)
|
||||
AddPermissionToPosition(ctx context.Context, positionID, permissionID uuid.UUID) error
|
||||
RemovePermissionFromPosition(ctx context.Context, positionID, permissionID uuid.UUID) error
|
||||
|
||||
GetPositionByCode(ctx context.Context, code string) (*models.Position, error)
|
||||
GetUserPositions(ctx context.Context, userID uuid.UUID, locale string) ([]models.PositionLocalized, error)
|
||||
GetAllPositions(ctx context.Context, locale string) ([]models.PositionLocalized, error)
|
||||
GetUsersWithPositions(ctx context.Context, locale string) ([]models.UserWithPositions, error)
|
||||
UpsertPositionForLocale(ctx context.Context, code, locale, name string, description *string) error
|
||||
|
||||
AssignPosition(ctx context.Context, userID, positionID uuid.UUID, assignedBy *uuid.UUID, validFrom time.Time, validTo *time.Time) error
|
||||
RevokePosition(ctx context.Context, userID, positionID uuid.UUID) error
|
||||
|
||||
UserHasPermission(ctx context.Context, userID uuid.UUID, permissionCode string) (bool, string, error)
|
||||
GetUserPermissions(ctx context.Context, userID uuid.UUID, locale string) ([]models.Permission, error)
|
||||
|
||||
CreatePermission(ctx context.Context, code, locale string, description *string) error
|
||||
UpdatePermission(ctx context.Context, code, locale string, description *string) error
|
||||
DeletePositionByCode(ctx context.Context, code string) error
|
||||
DeletePermission(ctx context.Context, code string) error
|
||||
}
|
||||
|
||||
type TokenConfig struct {
|
||||
AccessTTL time.Duration
|
||||
RefreshTTL time.Duration
|
||||
@@ -83,26 +63,23 @@ type ClientAuth struct {
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
users UserRepo
|
||||
oauth OAuthRepo
|
||||
positions PositionRepo
|
||||
redis *redis.Client
|
||||
tokens TokenConfig
|
||||
users UserRepo
|
||||
oauth OAuthRepo
|
||||
redis *redis.Client
|
||||
tokens TokenConfig
|
||||
}
|
||||
|
||||
func New(
|
||||
users UserRepo,
|
||||
oauth OAuthRepo,
|
||||
positions PositionRepo,
|
||||
rdb *redis.Client,
|
||||
tokenCfg TokenConfig,
|
||||
) *Service {
|
||||
return &Service{
|
||||
users: users,
|
||||
oauth: oauth,
|
||||
positions: positions,
|
||||
redis: rdb,
|
||||
tokens: tokenCfg,
|
||||
users: users,
|
||||
oauth: oauth,
|
||||
redis: rdb,
|
||||
tokens: tokenCfg,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user