Skip to content

Commit aeca2bc

Browse files
committed
ref: adopt strict mode for TS
1 parent a54d911 commit aeca2bc

9 files changed

Lines changed: 51 additions & 34 deletions

File tree

‎server/src/config/index.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Config } from "src/utils/config";
2+
13
export default {
24
default: {
35
REMEMBER_ME: false,
@@ -12,6 +14,6 @@ export default {
1214
OIDC_GRANT_TYPE: "authorization_code",
1315
OIDC_FAMILY_NAME_FIELD: "family_name",
1416
OIDC_GIVEN_NAME_FIELD: "given_name",
15-
},
17+
} as Config,
1618
validator() {},
1719
};

‎server/src/controllers/oidc.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Context } from "koa";
22
import { getJson, postForm } from "../utils/http";
33
import { randomUUID, randomBytes } from "node:crypto";
44
import pkceChallenge from "pkce-challenge";
5+
import { Config } from "src/utils/config";
56

6-
const REQUIRED_OIDC_FIELDS = [
7+
export const REQUIRED_OIDC_FIELDS: (keyof Config)[] = [
78
"OIDC_AUTHORIZATION_ENDPOINT",
89
"OIDC_TOKEN_ENDPOINT",
910
"OIDC_USER_INFO_ENDPOINT",
@@ -17,7 +18,7 @@ const REQUIRED_OIDC_FIELDS = [
1718
];
1819

1920
const configValidation = () => {
20-
const config = strapi.config.get<object>("plugin::strapi-plugin-sso");
21+
const config = strapi.config.get<Config>("plugin::strapi-plugin-sso");
2122
const missing = REQUIRED_OIDC_FIELDS.filter((key) => !config?.[key]);
2223
if (missing.length > 0) {
2324
throw new Error(`These are required: ${missing.join(", ")}.`);
@@ -127,11 +128,7 @@ const oidcSignInCallback = async (ctx: Context) => {
127128

128129
// Client-side authentication persistence and redirection
129130
const nonce = randomUUID();
130-
const html = oauthService.renderSignUpSuccess(
131-
jwtToken,
132-
activateUser,
133-
nonce,
134-
);
131+
const html = oauthService.renderSignUpSuccess(jwtToken, nonce);
135132
ctx.set("Content-Security-Policy", `script-src 'nonce-${nonce}'`);
136133
ctx.body = html;
137134
} catch (e) {

‎server/src/controllers/role.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export default ({ strapi }: { strapi: Core.Strapi }) => ({
1717
await roleService.setConfig(ctx.request.body as RoleConfig);
1818
ctx.send({}, 204);
1919
} catch (e) {
20+
if (!(e instanceof Error)) throw e;
2021
strapi.log.error(e);
2122
ctx.send({ error: e.message }, 400);
2223
}

‎server/src/services/__tests__/oauth.spec.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, test, expect, jest } from "@jest/globals";
22
import accepts from "accepts";
3-
import oauth, { SSO_ERROR_MESSAGES } from "../oauth";
3+
import oauth, { SsoError } from "../oauth";
44

55
const ctxFor = (acceptLanguage) => {
66
const headers = { "accept-language": acceptLanguage };
@@ -28,7 +28,7 @@ describe("oauth service", () => {
2828
const service = oauth({ strapi: mockStrapi });
2929

3030
describe("renderSignUpError", () => {
31-
test.each(Object.entries(SSO_ERROR_MESSAGES))(
31+
test.each(Object.entries(SsoError))(
3232
'code "%s" renders the correct message',
3333
(code, message) => {
3434
expect(service.renderSignUpError(code)).toContain(message);

‎server/src/services/oauth.ts‎

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
import { Core } from "@strapi/strapi";
1+
import { Core, UID } from "@strapi/strapi";
22
import strapiUtils from "@strapi/utils";
33
import { SetOption } from "cookies";
44
import generator from "generate-password";
55
import { Context } from "koa";
66
import { randomUUID } from "node:crypto";
7+
import { Config } from "src/utils/config";
8+
import { SsoErrorCode } from "src/utils/errors";
79
import { 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

1918
export 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", {}),

‎server/src/services/role.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const roleService = ({ strapi }: { strapi: Core.Strapi }) => ({
2424
.query("plugin::strapi-plugin-sso.roles")
2525
.create({ data: { expression } });
2626
},
27-
async resolveRole(userInfo) {
27+
async resolveRole(userInfo: any) {
2828
const config = await this.getConfig();
2929
if (!config?.expression) {
3030
return null;

‎server/src/utils/config.ts‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
export type Config = {
2+
OIDC_AUTHORIZATION_ENDPOINT: string;
3+
OIDC_TOKEN_ENDPOINT: string;
4+
OIDC_USER_INFO_ENDPOINT: string;
5+
OIDC_CLIENT_ID: string;
6+
OIDC_CLIENT_SECRET: string;
7+
OIDC_REDIRECT_URI: string;
8+
OIDC_SCOPES: string;
9+
OIDC_GRANT_TYPE: string;
10+
OIDC_FAMILY_NAME_FIELD: string;
11+
OIDC_GIVEN_NAME_FIELD: string;
12+
13+
REMEMBER_ME?: boolean;
14+
};

‎server/src/utils/errors.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { SsoError } from "src/services/oauth";
2+
3+
export type SsoErrorCode = keyof typeof SsoError;

‎server/tsconfig.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
"compilerOptions": {
55
"rootDir": "../",
66
"baseUrl": ".",
7+
"strict": true
78
}
89
}

0 commit comments

Comments
 (0)