d3b8cdba8f
* fix(sso): azure upn fallback * fix(sso): more explicit types * fix(sso): update redirects * fix(sso): safer property check
40 lines
1.1 KiB
TypeScript
40 lines
1.1 KiB
TypeScript
import {
|
|
OidcProfile,
|
|
SpeckleOidcProfile,
|
|
UserSsoSessionRecord
|
|
} from '@/modules/workspaces/domain/sso/types'
|
|
import { UnknownObject, UserinfoResponse } from 'openid-client'
|
|
|
|
/**
|
|
* Get the default expiration time for an SSO session based on the current time.
|
|
* TODO: Is 7 days a good default session length?
|
|
*/
|
|
export const getDefaultSsoSessionExpirationDate = (): Date => {
|
|
const now = new Date()
|
|
now.setDate(now.getDate() + 7)
|
|
return now
|
|
}
|
|
|
|
export const isValidSsoSession = (session: UserSsoSessionRecord): boolean => {
|
|
return session.validUntil.getTime() > new Date().getTime()
|
|
}
|
|
|
|
export const isValidOidcProfile = (
|
|
profile: UserinfoResponse<UnknownObject, UnknownObject>
|
|
): profile is OidcProfile => {
|
|
return !!profile.email || !!profile.upn
|
|
}
|
|
|
|
const isSpeckleOidcProfile = (
|
|
profile: OidcProfile
|
|
): profile is OidcProfile<SpeckleOidcProfile> => {
|
|
return Object.hasOwn(profile, 'email')
|
|
}
|
|
|
|
/**
|
|
* Special handling required in case we encounter Entra ID with a particular configuration.
|
|
*/
|
|
export const getEmailFromOidcProfile = (profile: OidcProfile): string => {
|
|
return isSpeckleOidcProfile(profile) ? profile.email : profile.upn
|
|
}
|