Files
probo/pkg/server/api/trust/v1/auth/auth.go
Sacha Al Himdani 994ec7b67f Change Authentification
Signed-off-by: Sacha Al Himdani <sacha@getprobo.com>
2025-08-06 16:34:43 +02:00

60 lines
2.0 KiB
Go

// Copyright (c) 2025 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package auth
import (
"context"
"fmt"
"github.com/99designs/gqlgen/graphql"
"github.com/getprobo/probo/pkg/coredata"
"github.com/getprobo/probo/pkg/gid"
"github.com/getprobo/probo/pkg/server/api/trust/v1/types"
)
type TokenAccessData struct {
TrustCenterID gid.GID
Email string
TenantID gid.TenantID
Scope string
}
type ContextAccessor interface {
UserFromContext(ctx context.Context) *coredata.User
TokenAccessFromContext(ctx context.Context) *TokenAccessData
}
func GetCurrentUserRole(ctx context.Context, accessor ContextAccessor) types.Role {
user := accessor.UserFromContext(ctx)
tokenAccess := accessor.TokenAccessFromContext(ctx)
if user != nil || tokenAccess != nil {
return types.RoleUser
}
return types.RoleNone
}
func MustBeAuthenticatedDirective(accessor ContextAccessor) func(ctx context.Context, obj interface{}, next graphql.Resolver, role *types.Role) (interface{}, error) {
return func(ctx context.Context, obj interface{}, next graphql.Resolver, role *types.Role) (interface{}, error) {
currentRole := GetCurrentUserRole(ctx, accessor)
if role != nil && *role == types.RoleUser && currentRole == types.RoleNone {
return nil, fmt.Errorf("access denied: authentication required")
}
return next(ctx)
}
}