2022-09-20 15:16:26 -05:00
package coderd
import (
2024-11-15 12:30:11 -06:00
"bytes"
2022-09-20 15:16:26 -05:00
"crypto/subtle"
2023-07-25 17:49:52 -05:00
"database/sql"
2022-09-20 15:16:26 -05:00
"encoding/json"
"net/http"
2024-10-25 18:27:34 -04:00
"time"
2022-09-20 15:16:26 -05:00
"github.com/go-chi/chi/v5"
"github.com/google/uuid"
"github.com/imulab/go-scim/pkg/v2/handlerutil"
scimjson "github.com/imulab/go-scim/pkg/v2/json"
"github.com/imulab/go-scim/pkg/v2/service"
"github.com/imulab/go-scim/pkg/v2/spec"
2023-07-25 17:49:52 -05:00
"golang.org/x/xerrors"
2022-09-20 15:16:26 -05:00
2023-08-18 13:55:43 -05:00
agpl "github.com/coder/coder/v2/coderd"
2024-06-20 17:22:27 -05:00
"github.com/coder/coder/v2/coderd/audit"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/dbauthz"
2023-09-01 19:50:12 +03:00
"github.com/coder/coder/v2/coderd/database/dbtime"
2023-08-18 13:55:43 -05:00
"github.com/coder/coder/v2/coderd/httpapi"
"github.com/coder/coder/v2/codersdk"
2024-10-25 18:27:34 -04:00
"github.com/coder/coder/v2/enterprise/coderd/scim"
2022-09-20 15:16:26 -05:00
)
func ( api * API ) scimVerifyAuthHeader ( r * http . Request ) bool {
2024-11-15 12:30:11 -06:00
bearer := [] byte ( "bearer " )
2022-09-20 15:16:26 -05:00
hdr := [] byte ( r . Header . Get ( "Authorization" ))
2024-11-15 12:30:11 -06:00
// Use toLower to make the comparison case-insensitive.
if len ( hdr ) >= len ( bearer ) && subtle . ConstantTimeCompare ( bytes . ToLower ( hdr [: len ( bearer )]), bearer ) == 1 {
2024-10-25 11:52:57 -05:00
hdr = hdr [ len ( bearer ):]
}
2022-09-20 15:16:26 -05:00
return len ( api . SCIMAPIKey ) != 0 && subtle . ConstantTimeCompare ( hdr , api . SCIMAPIKey ) == 1
}
2024-10-25 18:27:34 -04:00
2024-11-15 12:30:11 -06:00
func scimUnauthorized ( rw http . ResponseWriter ) {
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusUnauthorized , "invalidAuthorization" , xerrors . New ( "invalid authorization" )))
}
2024-10-25 18:27:34 -04:00
// scimServiceProviderConfig returns a static SCIM service provider configuration.
//
// @Summary SCIM 2.0: Service Provider Config
// @ID scim-get-service-provider-config
// @Produce application/scim+json
// @Tags Enterprise
// @Success 200
// @Router /scim/v2/ServiceProviderConfig [get]
func ( api * API ) scimServiceProviderConfig ( rw http . ResponseWriter , _ * http . Request ) {
// No auth needed to query this endpoint.
rw . Header (). Set ( "Content-Type" , spec . ApplicationScimJson )
rw . WriteHeader ( http . StatusOK )
// providerUpdated is the last time the static provider config was updated.
// Increment this time if you make any changes to the provider config.
providerUpdated := time . Date ( 2024 , 10 , 25 , 17 , 0 , 0 , 0 , time . UTC )
var location string
locURL , err := api . AccessURL . Parse ( "/scim/v2/ServiceProviderConfig" )
if err == nil {
location = locURL . String ()
}
enc := json . NewEncoder ( rw )
enc . SetEscapeHTML ( true )
_ = enc . Encode ( scim . ServiceProviderConfig {
Schemas : [] string { "urn:ietf:params:scim:schemas:core:2.0:ServiceProviderConfig" },
2024-12-17 13:20:01 -05:00
DocURI : "https://coder.com/docs/admin/users/oidc-auth#scim" ,
2024-10-25 18:27:34 -04:00
Patch : scim . Supported {
Supported : true ,
},
Bulk : scim . BulkSupported {
Supported : false ,
},
Filter : scim . FilterSupported {
Supported : false ,
},
ChangePassword : scim . Supported {
Supported : false ,
},
Sort : scim . Supported {
Supported : false ,
},
ETag : scim . Supported {
Supported : false ,
},
AuthSchemes : [] scim . AuthenticationScheme {
{
Type : "oauthbearertoken" ,
Name : "HTTP Header Authentication" ,
Description : "Authentication scheme using the Authorization header with the shared token" ,
2024-12-17 13:20:01 -05:00
DocURI : "https://coder.com/docs/admin/users/oidc-auth#scim" ,
2024-10-25 18:27:34 -04:00
},
},
Meta : scim . ServiceProviderMeta {
Created : providerUpdated ,
LastModified : providerUpdated ,
Location : location ,
ResourceType : "ServiceProviderConfig" ,
},
})
}
2022-09-20 15:16:26 -05:00
// scimGetUsers intentionally always returns no users. This is done to always force
// Okta to try and create each user individually, this way we don't need to
// implement fetching users twice.
//
2023-01-11 16:05:42 +01:00
// @Summary SCIM 2.0: Get users
// @ID scim-get-users
2024-10-25 12:40:13 -05:00
// @Security Authorization
2023-01-11 16:05:42 +01:00
// @Produce application/scim+json
// @Tags Enterprise
// @Success 200
2023-01-13 12:27:21 +01:00
// @Router /scim/v2/Users [get]
2023-01-11 16:05:42 +01:00
//
2022-09-20 15:16:26 -05:00
//nolint:revive
func ( api * API ) scimGetUsers ( rw http . ResponseWriter , r * http . Request ) {
if ! api . scimVerifyAuthHeader ( r ) {
2024-11-15 12:30:11 -06:00
scimUnauthorized ( rw )
2022-09-20 15:16:26 -05:00
return
}
_ = handlerutil . WriteSearchResultToResponse ( rw , & service . QueryResponse {
TotalResults : 0 ,
StartIndex : 1 ,
ItemsPerPage : 0 ,
Resources : [] scimjson . Serializable {},
})
}
// scimGetUser intentionally always returns an error saying the user wasn't found.
// This is done to always force Okta to try and create the user, this way we
// don't need to implement fetching users twice.
//
2023-01-11 16:05:42 +01:00
// @Summary SCIM 2.0: Get user by ID
// @ID scim-get-user-by-id
2024-10-25 12:40:13 -05:00
// @Security Authorization
2023-01-11 16:05:42 +01:00
// @Produce application/scim+json
// @Tags Enterprise
// @Param id path string true "User ID" format(uuid)
// @Failure 404
// @Router /scim/v2/Users/{id} [get]
//
2022-09-20 15:16:26 -05:00
//nolint:revive
func ( api * API ) scimGetUser ( rw http . ResponseWriter , r * http . Request ) {
if ! api . scimVerifyAuthHeader ( r ) {
2024-11-15 12:30:11 -06:00
scimUnauthorized ( rw )
2022-09-20 15:16:26 -05:00
return
}
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusNotFound , spec . ErrNotFound . Type , xerrors . New ( "endpoint will always return 404" )))
2022-09-20 15:16:26 -05:00
}
// We currently use our own struct instead of using the SCIM package. This was
// done mostly because the SCIM package was almost impossible to use. We only
// need these fields, so it was much simpler to use our own struct. This was
// tested only with Okta.
type SCIMUser struct {
Schemas [] string `json:"schemas"`
ID string `json:"id"`
UserName string `json:"userName"`
Name struct {
GivenName string `json:"givenName"`
FamilyName string `json:"familyName"`
} `json:"name"`
Emails [] struct {
Primary bool `json:"primary"`
2023-01-11 16:05:42 +01:00
Value string `json:"value" format:"email"`
2022-09-20 15:16:26 -05:00
Type string `json:"type"`
Display string `json:"display"`
} `json:"emails"`
2024-12-12 08:11:13 -06:00
// Active is a ptr to prevent the empty value from being interpreted as false.
Active * bool `json:"active"`
2022-09-20 15:16:26 -05:00
Groups [] interface {} `json:"groups"`
Meta struct {
ResourceType string `json:"resourceType"`
} `json:"meta"`
}
2024-06-20 17:22:27 -05:00
var SCIMAuditAdditionalFields = map [ string ] string {
"automatic_actor" : "coder" ,
"automatic_subsystem" : "scim" ,
}
2022-09-20 15:16:26 -05:00
// scimPostUser creates a new user, or returns the existing user if it exists.
2023-01-11 16:05:42 +01:00
//
// @Summary SCIM 2.0: Create new user
// @ID scim-create-new-user
2024-10-25 12:40:13 -05:00
// @Security Authorization
2023-01-11 16:05:42 +01:00
// @Produce json
// @Tags Enterprise
// @Param request body coderd.SCIMUser true "New user"
// @Success 200 {object} coderd.SCIMUser
// @Router /scim/v2/Users [post]
2022-09-20 15:16:26 -05:00
func ( api * API ) scimPostUser ( rw http . ResponseWriter , r * http . Request ) {
ctx := r . Context ()
if ! api . scimVerifyAuthHeader ( r ) {
2024-11-15 12:30:11 -06:00
scimUnauthorized ( rw )
2022-09-20 15:16:26 -05:00
return
}
2024-06-20 17:22:27 -05:00
auditor := * api . AGPL . Auditor . Load ()
aReq , commitAudit := audit . InitRequest [ database . User ]( rw , & audit . RequestParams {
Audit : auditor ,
Log : api . Logger ,
Request : r ,
Action : database . AuditActionCreate ,
AdditionalFields : SCIMAuditAdditionalFields ,
})
defer commitAudit ()
2022-09-20 15:16:26 -05:00
var sUser SCIMUser
err := json . NewDecoder ( r . Body ). Decode ( & sUser )
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidRequest" , err ))
2022-09-20 15:16:26 -05:00
return
}
2024-12-12 08:11:13 -06:00
if sUser . Active == nil {
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidRequest" , xerrors . New ( "active field is required" )))
return
}
2022-09-20 15:16:26 -05:00
email := ""
for _ , e := range sUser . Emails {
if e . Primary {
email = e . Value
break
}
}
if email == "" {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidEmail" , xerrors . New ( "no primary email provided" )))
2022-09-20 15:16:26 -05:00
return
}
2023-07-25 17:49:52 -05:00
//nolint:gocritic
2023-08-04 17:03:21 -05:00
dbUser , err := api . Database . GetUserByEmailOrUsername ( dbauthz . AsSystemRestricted ( ctx ), database . GetUserByEmailOrUsernameParams {
2023-07-25 17:49:52 -05:00
Email : email ,
Username : sUser . UserName ,
})
if err != nil && ! xerrors . Is ( err , sql . ErrNoRows ) {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , err ) // internal error
2023-07-25 17:49:52 -05:00
return
}
if err == nil {
2023-08-04 17:03:21 -05:00
sUser . ID = dbUser . ID . String ()
sUser . UserName = dbUser . Username
2024-12-12 08:11:13 -06:00
if * sUser . Active && dbUser . Status == database . UserStatusSuspended {
2023-08-04 17:03:21 -05:00
//nolint:gocritic
2024-06-20 17:22:27 -05:00
newUser , err := api . Database . UpdateUserStatus ( dbauthz . AsSystemRestricted ( r . Context ()), database . UpdateUserStatusParams {
2023-08-04 17:03:21 -05:00
ID : dbUser . ID ,
// The user will get transitioned to Active after logging in.
2025-12-17 16:49:40 +04:00
Status : database . UserStatusDormant ,
UpdatedAt : dbtime . Now (),
UserIsSeen : false ,
2023-08-04 17:03:21 -05:00
})
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , err ) // internal error
2023-08-04 17:03:21 -05:00
return
}
2024-06-20 17:22:27 -05:00
aReq . New = newUser
} else {
aReq . New = dbUser
2023-08-04 17:03:21 -05:00
}
2023-07-25 17:49:52 -05:00
2024-06-20 17:22:27 -05:00
aReq . Old = dbUser
2023-07-25 17:49:52 -05:00
httpapi . Write ( ctx , rw , http . StatusOK , sUser )
return
}
2023-06-08 17:59:49 -05:00
// The username is a required property in Coder. We make a best-effort
// attempt at using what the claims provide, but if that fails we will
// generate a random username.
2024-09-04 18:17:53 +10:00
usernameValid := codersdk . NameValid ( sUser . UserName )
2023-06-08 17:59:49 -05:00
if usernameValid != nil {
// If no username is provided, we can default to use the email address.
// This will be converted in the from function below, so it's safe
// to keep the domain.
if sUser . UserName == "" {
sUser . UserName = email
}
2024-09-04 18:17:53 +10:00
sUser . UserName = codersdk . UsernameFrom ( sUser . UserName )
2023-06-08 17:59:49 -05:00
}
2024-09-16 19:17:38 -05:00
// If organization sync is enabled, the user's organizations will be
// corrected on login. If including the default org, then always assign
// the default org, regardless if sync is enabled or not.
// This is to preserve single org deployment behavior.
organizations := [] uuid . UUID {}
2024-11-08 08:44:14 -06:00
//nolint:gocritic // SCIM operations are a system user
orgSync , err := api . IDPSync . OrganizationSyncSettings ( dbauthz . AsSystemRestricted ( ctx ), api . Database )
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusInternalServerError , "internalError" , xerrors . Errorf ( "failed to get organization sync settings: %w" , err )))
2024-11-08 08:44:14 -06:00
return
}
if orgSync . AssignDefault {
2024-09-16 19:17:38 -05:00
//nolint:gocritic // SCIM operations are a system user
defaultOrganization , err := api . Database . GetDefaultOrganization ( dbauthz . AsSystemRestricted ( ctx ))
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusInternalServerError , "internalError" , xerrors . Errorf ( "failed to get default organization: %w" , err )))
2024-09-16 19:17:38 -05:00
return
}
organizations = append ( organizations , defaultOrganization . ID )
2023-05-18 20:54:45 -04:00
}
2023-02-14 08:27:06 -06:00
//nolint:gocritic // needed for SCIM
2024-08-23 16:23:51 -05:00
dbUser , err = api . AGPL . CreateUser ( dbauthz . AsSystemRestricted ( ctx ), api . Database , agpl . CreateUserRequest {
CreateUserRequestWithOrgs : codersdk . CreateUserRequestWithOrgs {
Username : sUser . UserName ,
Email : email ,
2024-09-16 19:17:38 -05:00
OrganizationIDs : organizations ,
2022-09-20 15:16:26 -05:00
},
LoginType : database . LoginTypeOIDC ,
2024-07-30 15:37:45 +02:00
// Do not send notifications to user admins as SCIM endpoint might be called sequentially to all users.
SkipNotifications : true ,
2022-09-20 15:16:26 -05:00
})
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusInternalServerError , "internalError" , xerrors . Errorf ( "failed to create user: %w" , err )))
2022-09-20 15:16:26 -05:00
return
}
2024-06-20 17:22:27 -05:00
aReq . New = dbUser
aReq . UserID = dbUser . ID
2022-09-20 15:16:26 -05:00
2023-08-04 17:03:21 -05:00
sUser . ID = dbUser . ID . String ()
sUser . UserName = dbUser . Username
2022-09-20 15:16:26 -05:00
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusOK , sUser )
2022-09-20 15:16:26 -05:00
}
// scimPatchUser supports suspending and activating users only.
2023-01-11 16:05:42 +01:00
//
// @Summary SCIM 2.0: Update user account
// @ID scim-update-user-status
2024-10-25 12:40:13 -05:00
// @Security Authorization
2023-01-11 16:05:42 +01:00
// @Produce application/scim+json
// @Tags Enterprise
// @Param id path string true "User ID" format(uuid)
// @Param request body coderd.SCIMUser true "Update user request"
// @Success 200 {object} codersdk.User
// @Router /scim/v2/Users/{id} [patch]
2022-09-20 15:16:26 -05:00
func ( api * API ) scimPatchUser ( rw http . ResponseWriter , r * http . Request ) {
ctx := r . Context ()
if ! api . scimVerifyAuthHeader ( r ) {
2024-11-15 12:30:11 -06:00
scimUnauthorized ( rw )
2022-09-20 15:16:26 -05:00
return
}
2024-06-20 17:22:27 -05:00
auditor := * api . AGPL . Auditor . Load ()
2024-07-19 11:30:02 -10:00
aReq , commitAudit := audit . InitRequestWithCancel [ database . User ]( rw , & audit . RequestParams {
2024-06-20 17:22:27 -05:00
Audit : auditor ,
Log : api . Logger ,
Request : r ,
Action : database . AuditActionWrite ,
})
2024-07-19 11:30:02 -10:00
defer commitAudit ( true )
2024-06-20 17:22:27 -05:00
2022-09-20 15:16:26 -05:00
id := chi . URLParam ( r , "id" )
var sUser SCIMUser
err := json . NewDecoder ( r . Body ). Decode ( & sUser )
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidRequest" , err ))
2022-09-20 15:16:26 -05:00
return
}
sUser . ID = id
uid , err := uuid . Parse ( id )
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidId" , xerrors . Errorf ( "id must be a uuid: %w" , err )))
2022-09-20 15:16:26 -05:00
return
}
2023-02-14 08:27:06 -06:00
//nolint:gocritic // needed for SCIM
2023-02-15 16:14:37 +00:00
dbUser , err := api . Database . GetUserByID ( dbauthz . AsSystemRestricted ( ctx ), uid )
2022-09-20 15:16:26 -05:00
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , err ) // internal error
2022-09-20 15:16:26 -05:00
return
}
2024-06-20 17:22:27 -05:00
aReq . Old = dbUser
aReq . UserID = dbUser . ID
2022-09-20 15:16:26 -05:00
2024-12-12 08:11:13 -06:00
if sUser . Active == nil {
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidRequest" , xerrors . New ( "active field is required" )))
return
2022-09-20 15:16:26 -05:00
}
2024-12-12 08:11:13 -06:00
newStatus := scimUserStatus ( dbUser , * sUser . Active )
if dbUser . Status != newStatus {
2024-07-19 11:30:02 -10:00
//nolint:gocritic // needed for SCIM
userNew , err := api . Database . UpdateUserStatus ( dbauthz . AsSystemRestricted ( r . Context ()), database . UpdateUserStatusParams {
2025-12-17 16:49:40 +04:00
ID : dbUser . ID ,
Status : newStatus ,
UpdatedAt : dbtime . Now (),
UserIsSeen : false ,
2024-07-19 11:30:02 -10:00
})
if err != nil {
2024-11-15 12:30:11 -06:00
_ = handlerutil . WriteError ( rw , err ) // internal error
2024-07-19 11:30:02 -10:00
return
}
dbUser = userNew
} else {
// Do not push an audit log if there is no change.
commitAudit ( false )
2022-09-20 15:16:26 -05:00
}
2024-07-19 11:30:02 -10:00
aReq . New = dbUser
2022-09-21 17:07:00 -05:00
httpapi . Write ( ctx , rw , http . StatusOK , sUser )
2022-09-20 15:16:26 -05:00
}
2024-12-12 08:11:13 -06:00
// scimPutUser supports suspending and activating users only.
// TODO: SCIM specification requires that the PUT method should replace the entire user object.
// At present, our fields read as 'immutable' except for the 'active' field.
// See: https://datatracker.ietf.org/doc/html/rfc7644#section-3.5.1
//
// @Summary SCIM 2.0: Replace user account
// @ID scim-replace-user-status
// @Security Authorization
// @Produce application/scim+json
// @Tags Enterprise
// @Param id path string true "User ID" format(uuid)
// @Param request body coderd.SCIMUser true "Replace user request"
// @Success 200 {object} codersdk.User
// @Router /scim/v2/Users/{id} [put]
func ( api * API ) scimPutUser ( rw http . ResponseWriter , r * http . Request ) {
ctx := r . Context ()
if ! api . scimVerifyAuthHeader ( r ) {
scimUnauthorized ( rw )
return
}
auditor := * api . AGPL . Auditor . Load ()
aReq , commitAudit := audit . InitRequestWithCancel [ database . User ]( rw , & audit . RequestParams {
Audit : auditor ,
Log : api . Logger ,
Request : r ,
Action : database . AuditActionWrite ,
})
defer commitAudit ( true )
id := chi . URLParam ( r , "id" )
var sUser SCIMUser
err := json . NewDecoder ( r . Body ). Decode ( & sUser )
if err != nil {
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidRequest" , err ))
return
}
sUser . ID = id
if sUser . Active == nil {
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidRequest" , xerrors . New ( "active field is required" )))
return
}
uid , err := uuid . Parse ( id )
if err != nil {
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "invalidId" , xerrors . Errorf ( "id must be a uuid: %w" , err )))
return
}
//nolint:gocritic // needed for SCIM
dbUser , err := api . Database . GetUserByID ( dbauthz . AsSystemRestricted ( ctx ), uid )
if err != nil {
_ = handlerutil . WriteError ( rw , err ) // internal error
return
}
aReq . Old = dbUser
aReq . UserID = dbUser . ID
// Technically our immutability rules dictate that we should not allow
// fields to be changed. According to the SCIM specification, this error should
// be returned.
// This immutability enforcement only exists because we have not implemented it
// yet. If these rules are causing errors, this code should be updated to allow
// the fields to be changed.
// TODO: Currently ignoring a lot of the SCIM fields. Coder's SCIM implementation
// is very basic and only supports active status changes.
if immutabilityViolation ( dbUser . Username , sUser . UserName ) {
_ = handlerutil . WriteError ( rw , scim . NewHTTPError ( http . StatusBadRequest , "mutability" , xerrors . Errorf ( "username is currently an immutable field, and cannot be changed. Current: %s, New: %s" , dbUser . Username , sUser . UserName )))
return
}
newStatus := scimUserStatus ( dbUser , * sUser . Active )
if dbUser . Status != newStatus {
//nolint:gocritic // needed for SCIM
userNew , err := api . Database . UpdateUserStatus ( dbauthz . AsSystemRestricted ( r . Context ()), database . UpdateUserStatusParams {
2025-12-17 16:49:40 +04:00
ID : dbUser . ID ,
Status : newStatus ,
UpdatedAt : dbtime . Now (),
UserIsSeen : false ,
2024-12-12 08:11:13 -06:00
})
if err != nil {
_ = handlerutil . WriteError ( rw , err ) // internal error
return
}
dbUser = userNew
} else {
// Do not push an audit log if there is no change.
commitAudit ( false )
}
aReq . New = dbUser
httpapi . Write ( ctx , rw , http . StatusOK , sUser )
}
2025-03-26 01:56:39 -05:00
func immutabilityViolation [ T comparable ]( old , newVal T ) bool {
2024-12-12 08:11:13 -06:00
var empty T
2025-03-26 01:56:39 -05:00
if newVal == empty {
2024-12-12 08:11:13 -06:00
// No change
return false
}
2025-03-26 01:56:39 -05:00
return old != newVal
2024-12-12 08:11:13 -06:00
}
//nolint:revive // active is not a control flag
func scimUserStatus ( user database . User , active bool ) database . UserStatus {
if ! active {
return database . UserStatusSuspended
}
switch user . Status {
case database . UserStatusActive :
// Keep the user active
return database . UserStatusActive
case database . UserStatusDormant , database . UserStatusSuspended :
// Move (or keep) as dormant
return database . UserStatusDormant
default :
// If the status is unknown, just move them to dormant.
// The user will get transitioned to Active after logging in.
return database . UserStatusDormant
}
}