1- import { Core } from "@strapi/strapi" ;
1+ import { Core , UID } from "@strapi/strapi" ;
22import strapiUtils from "@strapi/utils" ;
33import { SetOption } from "cookies" ;
44import generator from "generate-password" ;
55import { Context } from "koa" ;
66import { randomUUID } from "node:crypto" ;
7+ import { Config } from "src/utils/config" ;
8+ import { SsoErrorCode } from "src/utils/errors" ;
79import { AdminSessionsConfig } from "src/types/strapi" ;
810
9- export const SSO_ERROR_MESSAGES = Object . freeze ( {
10- sso_no_code : "No authorization code was returned by the provider." ,
11- sso_invalid_state :
12- "The login request could not be verified. Please try again." ,
13- sso_access_denied :
14- "Your account has not been granted access. Please contact your administrator." ,
15- sso_failed :
16- "Authentication failed. Please try again or contact your administrator." ,
17- } ) ;
11+ export enum SsoError {
12+ sso_no_code = "No authorization code was returned by the provider." ,
13+ sso_invalid_state = "The login request could not be verified. Please try again." ,
14+ sso_access_denied = "Your account has not been granted access. Please contact your administrator." ,
15+ sso_failed = "Authentication failed. Please try again or contact your administrator." ,
16+ }
1817
1918export default ( { strapi } : { strapi : Core . Strapi } ) => ( {
2019 async createUser (
2120 email : string ,
2221 lastname : string ,
2322 firstname : string ,
24- locale ,
23+ locale : string ,
2524 roles = [ ] ,
2625 ) {
2726 const userService = strapi . service ( "admin::user" ) ;
@@ -59,23 +58,23 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
5958 localeFindByHeader ( ctx : Context ) {
6059 return ctx . acceptsLanguages ( "en" , "fr" ) || "en" ;
6160 } ,
62- async triggerWebHook ( user ) {
61+ async triggerWebHook ( user : any ) {
6362 const eventHub = strapi . eventHub ;
6463 if ( ! eventHub ) return ;
6564
66- const modelDef = strapi . getModel ( "admin::user" ) ;
65+ const schema = strapi . getModel ( "admin::user" ) ;
6766 const sanitizedEntity =
6867 await strapiUtils . sanitize . sanitizers . defaultSanitizeOutput (
69- { schema : modelDef , getModel : strapi . getModel . bind ( strapi ) } ,
68+ { schema, getModel : ( uid ) => strapi . getModel ( uid as UID . Schema ) } ,
7069 user ,
7170 ) ;
7271
7372 eventHub . emit ( "entry.create" , {
74- model : modelDef . modelName ,
73+ model : schema . modelName ,
7574 entry : sanitizedEntity ,
7675 } ) ;
7776 } ,
78- triggerSignInSuccess ( user ) {
77+ triggerSignInSuccess ( user : any ) {
7978 const { password, ...safeUser } = user ;
8079 const eventHub = strapi . eventHub ;
8180 eventHub . emit ( "admin.auth.success" , {
@@ -84,9 +83,9 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
8483 } ) ;
8584 } ,
8685 // Sign In Success
87- renderSignUpSuccess ( jwtToken , user , nonce ) {
86+ renderSignUpSuccess ( jwtToken : string , nonce : string ) {
8887 // get REMEMBER_ME from config
89- const config = strapi . config . get ( "plugin::strapi-plugin-sso" ) ;
88+ const config : Config = strapi . config . get ( "plugin::strapi-plugin-sso" ) ;
9089 const REMEMBER_ME = config [ "REMEMBER_ME" ] ;
9190 const isRememberMe = ! ! REMEMBER_ME ;
9291
@@ -113,8 +112,8 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
113112</body>
114113</html>` ;
115114 } ,
116- renderSignUpError ( code ) {
117- const message = SSO_ERROR_MESSAGES [ code ] ?? SSO_ERROR_MESSAGES . sso_failed ;
115+ renderSignUpError ( code : SsoErrorCode ) {
116+ const message = SsoError [ code ] ?? SsoError . sso_failed ;
118117 const loginUrl = `${ strapi . config . admin . url } /auth/login` ;
119118 return `
120119<!doctype html>
@@ -143,7 +142,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
143142</body>
144143</html>` ;
145144 } ,
146- async generateToken ( user , ctx : Context ) {
145+ async generateToken ( user : any , ctx : Context ) {
147146 const sessionManager = strapi . sessionManager ;
148147 if ( ! sessionManager ) {
149148 throw new Error (
@@ -163,7 +162,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
163162 } ) ;
164163 }
165164
166- const config = strapi . config . get ( "plugin::strapi-plugin-sso" ) ;
165+ const config : Config = strapi . config . get ( "plugin::strapi-plugin-sso" ) ;
167166 const REMEMBER_ME = config [ "REMEMBER_ME" ] ;
168167 const rememberMe = ! ! REMEMBER_ME ;
169168
@@ -177,7 +176,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
177176 "admin.auth.sessions" ,
178177 { } ,
179178 ) ;
180- const maxRefresh = sessions . maxRefreshTokenLifespan ;
179+ const maxRefresh = sessions . maxRefreshTokenLifespan ?? 0 ;
181180 const cookieOptions : SetOption = {
182181 sameSite : "lax" ,
183182 ...strapi . config . get ( "admin.auth.cookie" , { } ) ,
0 commit comments