@scaleway/auth-scw 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -5
- package/dist/constants.d.ts +2 -4
- package/dist/index.d.ts +1 -0
- package/dist/types/iam/index.d.ts +2 -4
- package/dist/types/iam/types.d.ts +6 -7
- package/dist/types/index.d.ts +12 -18
- package/dist/useAuthScw/AuthScwProvider.d.ts +4 -15
- package/dist/useAuthScw/AuthScwProvider.js +1 -1
- package/dist/useAuthScw/authStoreManager.d.ts +5 -9
- package/dist/useAuthScw/authStoreManager.js +1 -1
- package/dist/useAuthScw/createClient.js +1 -1
- package/dist/zodSchemas.d.ts +3 -5
- package/dist/zodSchemas.js +3 -2
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ pnpm add @scaleway/auth-scw
|
|
|
16
16
|
|
|
17
17
|
### Prerequisites
|
|
18
18
|
|
|
19
|
-
```
|
|
19
|
+
```text
|
|
20
20
|
cd packages/auth-scw
|
|
21
21
|
```
|
|
22
22
|
|
|
@@ -27,15 +27,15 @@ pnpm build # Build the package
|
|
|
27
27
|
pnpm watch # Build the package and watch for changes
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
## Repo pattern
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
### How does renew token works? Single or multiple organisation
|
|
33
33
|
|
|
34
34
|
**Reminder:** A COOKIE is used to syncronise jwt between tabs
|
|
35
35
|
|
|
36
36
|
When users open severals tabs, syncronoisation of token between tabs can become tedious, here are the scenarios:
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
#### Single organisation
|
|
39
39
|
|
|
40
40
|
Let say you have 2 tabs on the same organisation, `tab1`, `tab2` with the same JWT, let's call it, `JWT_A`
|
|
41
41
|
|
|
@@ -56,7 +56,7 @@ Let say you have 2 tabs on the same organisation, `tab1`, `tab2` with the same J
|
|
|
56
56
|
1. A renew request on `tab1` is attempted with the last JWT from cookie (so JWT_B) and also because both tabs are on the same organisation therefore, the same jwt can be used for both.
|
|
57
57
|
2. the renew happen without issue and both tabs can be used without problems
|
|
58
58
|
|
|
59
|
-
|
|
59
|
+
#### Multiple organisation
|
|
60
60
|
|
|
61
61
|
Alright, read the Single organisation scenario if you haven't.
|
|
62
62
|
Here let's consider we have `tab1` and `tab2` on `Orga_A` with `JWT_A` and `tab3` on `Orga_B` with `JWT_B`:
|
package/dist/constants.d.ts
CHANGED
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AudienceIdType, AuthScwContextType, EncodedJWT } from "./types/index.js";
|
|
2
2
|
import { EmptySession } from "./constants.js";
|
|
3
3
|
import { AuthScwProvider, useAuthScw } from "./useAuthScw/AuthScwProvider.js";
|
|
4
|
+
import "./useAuthScw/index.js";
|
|
4
5
|
import { AuthStoreManager } from "./useAuthScw/authStoreManager.js";
|
|
5
6
|
export { type AudienceIdType, type AuthScwContextType, AuthScwProvider, AuthStoreManager, EmptySession, type EncodedJWT, useAuthScw };
|
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import { API, DeleteJWTRequest, EncodedJWT, JWT, UnauthenticatedAPI, UnauthenticatedApiRenewJWTRequest } from "./types.js";
|
|
2
|
-
|
|
3
2
|
//#region src/types/iam/index.d.ts
|
|
4
|
-
declare namespace Iamv1alpha1 {
|
|
3
|
+
export declare namespace Iamv1alpha1 {
|
|
5
4
|
export type { API, UnauthenticatedAPI };
|
|
6
5
|
export type { DeleteJWTRequest, EncodedJWT, JWT, UnauthenticatedApiRenewJWTRequest };
|
|
7
6
|
}
|
|
8
|
-
//#endregion
|
|
9
|
-
export { Iamv1alpha1 };
|
|
7
|
+
//#endregion
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { API } from "@scaleway/sdk-client";
|
|
2
2
|
import { Iamv1alpha1 } from "@scaleway/sdk-iam";
|
|
3
|
-
|
|
4
3
|
//#region src/types/iam/types.d.ts
|
|
5
|
-
type JWT = Iamv1alpha1.JWT;
|
|
6
|
-
type EncodedJWT = Iamv1alpha1.EncodedJWT;
|
|
7
|
-
type DeleteJWTRequest = Iamv1alpha1.DeleteJWTRequest;
|
|
8
|
-
type UnauthenticatedApiRenewJWTRequest = {
|
|
4
|
+
export type JWT = Iamv1alpha1.JWT;
|
|
5
|
+
export type EncodedJWT = Iamv1alpha1.EncodedJWT;
|
|
6
|
+
export type DeleteJWTRequest = Iamv1alpha1.DeleteJWTRequest;
|
|
7
|
+
export type UnauthenticatedApiRenewJWTRequest = {
|
|
9
8
|
/**
|
|
10
9
|
* The JWT ID of the JWT to renew.
|
|
11
10
|
*/
|
|
@@ -31,7 +30,7 @@ declare class API$1 extends API {
|
|
|
31
30
|
/**
|
|
32
31
|
* Unauthenticated API.
|
|
33
32
|
*/
|
|
34
|
-
declare class UnauthenticatedAPI extends API {
|
|
33
|
+
export declare class UnauthenticatedAPI extends API {
|
|
35
34
|
/**
|
|
36
35
|
* Renew a JWT.
|
|
37
36
|
*
|
|
@@ -41,4 +40,4 @@ declare class UnauthenticatedAPI extends API {
|
|
|
41
40
|
renewJWT: (request: Readonly<UnauthenticatedApiRenewJWTRequest>) => Promise<EncodedJWT>;
|
|
42
41
|
}
|
|
43
42
|
//#endregion
|
|
44
|
-
export { API$1 as API
|
|
43
|
+
export { API$1 as API };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -2,11 +2,10 @@ import { Iamv1alpha1 } from "./iam/index.js";
|
|
|
2
2
|
import { audienceIdSchema, jwtSchema } from "../zodSchemas.js";
|
|
3
3
|
import { z } from "zod";
|
|
4
4
|
import { API, Client, Settings } from "@scaleway/sdk-client";
|
|
5
|
-
|
|
6
5
|
//#region src/types/index.d.ts
|
|
7
|
-
type EncodedJWT = z.infer<typeof jwtSchema>;
|
|
8
|
-
type AudienceIdType = z.infer<typeof audienceIdSchema>;
|
|
9
|
-
type OnError = (onError: {
|
|
6
|
+
export type EncodedJWT = z.infer<typeof jwtSchema>;
|
|
7
|
+
export type AudienceIdType = z.infer<typeof audienceIdSchema>;
|
|
8
|
+
export type OnError = (onError: {
|
|
10
9
|
title: string;
|
|
11
10
|
error: unknown;
|
|
12
11
|
extra?: Record<string, unknown>;
|
|
@@ -15,7 +14,7 @@ type GetJWT = (audienceId?: string) => Promise<(EncodedJWT & {
|
|
|
15
14
|
source: 'refreshSession' | 'cookie';
|
|
16
15
|
}) | undefined>;
|
|
17
16
|
type GetJwtTokenResponse = NonNullable<Awaited<ReturnType<GetJWT>>>['token'] | undefined;
|
|
18
|
-
type AuthScwContextType = {
|
|
17
|
+
export type AuthScwContextType = {
|
|
19
18
|
audienceId?: string;
|
|
20
19
|
setAudienceId: (audienceId: string) => void;
|
|
21
20
|
jti?: string;
|
|
@@ -27,28 +26,23 @@ type AuthScwContextType = {
|
|
|
27
26
|
logout: () => void;
|
|
28
27
|
authenticated: boolean;
|
|
29
28
|
};
|
|
30
|
-
declare class IamV1alpha1API extends API {
|
|
29
|
+
export declare class IamV1alpha1API extends API {
|
|
31
30
|
deleteJWT: Iamv1alpha1.API['deleteJWT'];
|
|
32
31
|
}
|
|
33
|
-
declare class IamV1alpha1UnauthenticatedAPI extends API {
|
|
32
|
+
export declare class IamV1alpha1UnauthenticatedAPI extends API {
|
|
34
33
|
renewJWT: Iamv1alpha1.UnauthenticatedAPI['renewJWT'];
|
|
35
34
|
}
|
|
36
|
-
type ConfigurationClientAPIs = {
|
|
37
|
-
IamV1Alpha1:
|
|
38
|
-
|
|
39
|
-
};
|
|
40
|
-
IamUnauthenticatedV1Alpha1: {
|
|
41
|
-
new (client: Client): IamV1alpha1UnauthenticatedAPI;
|
|
42
|
-
};
|
|
35
|
+
export type ConfigurationClientAPIs = {
|
|
36
|
+
IamV1Alpha1: new (client: Client) => IamV1alpha1API;
|
|
37
|
+
IamUnauthenticatedV1Alpha1: new (client: Client) => IamV1alpha1UnauthenticatedAPI;
|
|
43
38
|
};
|
|
44
|
-
type ConfigAuthProvider = {
|
|
39
|
+
export type ConfigAuthProvider = {
|
|
45
40
|
clientSettings: Settings;
|
|
46
41
|
} & ConfigurationClientAPIs;
|
|
47
|
-
type CookieConfigType = {
|
|
42
|
+
export type CookieConfigType = {
|
|
48
43
|
httpOnly: boolean;
|
|
49
44
|
path: string;
|
|
50
45
|
sameSite: 'lax' | 'none' | 'strict' | boolean | undefined;
|
|
51
46
|
secure: boolean;
|
|
52
47
|
};
|
|
53
|
-
//#endregion
|
|
54
|
-
export { AudienceIdType, AuthScwContextType, ConfigAuthProvider, ConfigurationClientAPIs, CookieConfigType, EncodedJWT, IamV1alpha1API, IamV1alpha1UnauthenticatedAPI, OnError };
|
|
48
|
+
//#endregion
|
|
@@ -1,24 +1,13 @@
|
|
|
1
1
|
import { AuthScwContextType, ConfigAuthProvider, CookieConfigType, OnError } from "../types/index.js";
|
|
2
2
|
import { ReactNode } from "react";
|
|
3
|
-
|
|
4
3
|
//#region src/useAuthScw/AuthScwProvider.d.ts
|
|
5
|
-
type AuthProviderParamType = ConfigAuthProvider & {
|
|
4
|
+
export type AuthProviderParamType = ConfigAuthProvider & {
|
|
6
5
|
children: ReactNode;
|
|
7
6
|
cookieSuffix: string;
|
|
8
7
|
urlParamTokenName: string;
|
|
9
8
|
onError?: OnError;
|
|
10
9
|
cookieConfig?: CookieConfigType;
|
|
11
10
|
};
|
|
12
|
-
declare const useAuthScw: () => AuthScwContextType;
|
|
13
|
-
declare const AuthScwProvider: ({
|
|
14
|
-
|
|
15
|
-
IamV1Alpha1,
|
|
16
|
-
IamUnauthenticatedV1Alpha1,
|
|
17
|
-
children,
|
|
18
|
-
cookieSuffix,
|
|
19
|
-
cookieConfig,
|
|
20
|
-
urlParamTokenName,
|
|
21
|
-
onError
|
|
22
|
-
}: AuthProviderParamType) => import("react").JSX.Element;
|
|
23
|
-
//#endregion
|
|
24
|
-
export { AuthProviderParamType, AuthScwProvider, useAuthScw };
|
|
11
|
+
export declare const useAuthScw: () => AuthScwContextType;
|
|
12
|
+
export declare const AuthScwProvider: ({ clientSettings, IamV1Alpha1, IamUnauthenticatedV1Alpha1, children, cookieSuffix, cookieConfig, urlParamTokenName, onError }: AuthProviderParamType) => import("react").JSX.Element;
|
|
13
|
+
//#endregion
|
|
@@ -96,7 +96,7 @@ const AuthScwProvider = ({ clientSettings, IamV1Alpha1, IamUnauthenticatedV1Alph
|
|
|
96
96
|
if (cookieJWT?.jwt) return cookieJWT.jwt.jti;
|
|
97
97
|
}
|
|
98
98
|
}, [currentAudienceId]);
|
|
99
|
-
const isAuthenticated = useMemo(() =>
|
|
99
|
+
const isAuthenticated = useMemo(() => Boolean(currentAudienceId && getCookieJWT(currentAudienceId)), [currentAudienceId]);
|
|
100
100
|
const setAudienceId = useCallback((audienceId) => {
|
|
101
101
|
setCurrentAudienceId(audienceId);
|
|
102
102
|
}, []);
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AudienceIdType, EncodedJWT } from "../types/index.js";
|
|
2
|
-
|
|
3
2
|
//#region src/useAuthScw/authStoreManager.d.ts
|
|
4
|
-
declare const
|
|
3
|
+
declare const getUniqueHostnameString: (inputString: string) => string;
|
|
4
|
+
export declare const AuthStoreManager: {
|
|
5
5
|
COOKIE_AGE: number;
|
|
6
6
|
DOMAIN: string;
|
|
7
7
|
deleteAllJwts(): void;
|
|
@@ -11,13 +11,10 @@ declare const AuthStoreManager: {
|
|
|
11
11
|
getJwt(audienceId: string): EncodedJWT | null;
|
|
12
12
|
getKeyAudienceId(): string;
|
|
13
13
|
getKeySession(audienceId: string): string;
|
|
14
|
-
getUniqueHostnameString:
|
|
14
|
+
getUniqueHostnameString: typeof getUniqueHostnameString;
|
|
15
15
|
SUFFIX_KEY: string;
|
|
16
16
|
setAudienceId(audienceId: string): void;
|
|
17
|
-
setJwt({
|
|
18
|
-
jwtInfo,
|
|
19
|
-
setAudienceId
|
|
20
|
-
}: {
|
|
17
|
+
setJwt({ jwtInfo, setAudienceId }: {
|
|
21
18
|
jwtInfo: EncodedJWT;
|
|
22
19
|
setAudienceId?: boolean;
|
|
23
20
|
}): void;
|
|
@@ -25,5 +22,4 @@ declare const AuthStoreManager: {
|
|
|
25
22
|
typeGuardAudienceId(untypedObject: unknown): untypedObject is AudienceIdType;
|
|
26
23
|
typeGuardJWT(untypedObject: unknown): untypedObject is EncodedJWT;
|
|
27
24
|
};
|
|
28
|
-
//#endregion
|
|
29
|
-
export { AuthStoreManager };
|
|
25
|
+
//#endregion
|
|
@@ -5,7 +5,7 @@ import { parseCookie, stringifySetCookie } from "cookie";
|
|
|
5
5
|
const KEY_SESSION = "_scw_session";
|
|
6
6
|
const KEY_AUDIENCE_ID = "_scw_audience_id";
|
|
7
7
|
const DEFAULT_SUFFIX_KEY = "";
|
|
8
|
-
const COOKIE_AGE =
|
|
8
|
+
const COOKIE_AGE = 2678400;
|
|
9
9
|
let cookieConfig = COOKIE_CONFIG;
|
|
10
10
|
const DOMAIN = typeof window !== "undefined" ? globalThis.location.hostname : "";
|
|
11
11
|
const getUniqueHostnameString = (inputString) => inputString.replaceAll(".", "_").replaceAll("-", "_");
|
|
@@ -10,7 +10,7 @@ const clientSingleton = {
|
|
|
10
10
|
this.clientSettings = clientSettings;
|
|
11
11
|
},
|
|
12
12
|
createClient({ getAsyncToken }) {
|
|
13
|
-
if (!this.IamV1Alpha1 || !this.IamUnauthenticatedV1Alpha1 || !this.clientSettings) throw "You must set apis and client settings to use package: auth-scw";
|
|
13
|
+
if (!this.IamV1Alpha1 || !this.IamUnauthenticatedV1Alpha1 || !this.clientSettings) throw new Error("You must set apis and client settings to use package: auth-scw");
|
|
14
14
|
const client = createAdvancedClient(withProfile(this.clientSettings), withAdditionalInterceptors([...this.clientSettings.interceptors, { request: async ({ request }) => getAsyncToken ? addAsyncHeaderInterceptor(SESSION_HEADER_KEY, getAsyncToken)({ request }) : request }]));
|
|
15
15
|
const iamv1alpha1API = new this.IamV1Alpha1(client);
|
|
16
16
|
const iamUnauthenticatedV1Alpha1API = new this.IamUnauthenticatedV1Alpha1(client);
|
package/dist/zodSchemas.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Iamv1alpha1 } from "./types/iam/index.js";
|
|
2
2
|
import { z } from "zod";
|
|
3
|
-
|
|
4
3
|
//#region src/zodSchemas.d.ts
|
|
5
4
|
type TransformJWT = Omit<Iamv1alpha1.JWT, 'createdAt' | 'updatedAt' | 'expiresAt'> & {
|
|
6
5
|
createdAt?: string | Date;
|
|
@@ -12,9 +11,8 @@ type ValidateJWTSchema = {
|
|
|
12
11
|
token: string;
|
|
13
12
|
renewToken: string;
|
|
14
13
|
};
|
|
15
|
-
declare const jwtSchema: z.ZodType<ValidateJWTSchema
|
|
16
|
-
declare const audienceIdSchema: z.ZodObject<{
|
|
14
|
+
export declare const jwtSchema: z.ZodType<ValidateJWTSchema, unknown, z.core.$ZodTypeInternals<ValidateJWTSchema, unknown>>;
|
|
15
|
+
export declare const audienceIdSchema: z.ZodObject<{
|
|
17
16
|
audienceId: z.ZodString;
|
|
18
17
|
}, z.core.$strip>;
|
|
19
|
-
//#endregion
|
|
20
|
-
export { audienceIdSchema, jwtSchema };
|
|
18
|
+
//#endregion
|
package/dist/zodSchemas.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { z } from "zod";
|
|
2
2
|
//#region src/zodSchemas.ts
|
|
3
|
-
const
|
|
3
|
+
const rawJwtSchema = z.object({
|
|
4
4
|
jwt: z.object({
|
|
5
5
|
audienceId: z.string().min(1),
|
|
6
6
|
createdAt: z.union([z.string().min(1), z.date()]).optional(),
|
|
@@ -14,6 +14,7 @@ const jwtSchema = z.object({
|
|
|
14
14
|
renewToken: z.string().min(1),
|
|
15
15
|
token: z.string().min(1)
|
|
16
16
|
});
|
|
17
|
-
const
|
|
17
|
+
const jwtSchema = z.compile(rawJwtSchema);
|
|
18
|
+
const audienceIdSchema = z.compile(z.object({ audienceId: z.string().min(1) }));
|
|
18
19
|
//#endregion
|
|
19
20
|
export { audienceIdSchema, jwtSchema };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@scaleway/auth-scw",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Auth Scaleway",
|
|
5
5
|
"homepage": "https://github.com/scaleway/scaleway-lib/tree/main/packages/auth-scw",
|
|
6
6
|
"bugs": {
|
|
@@ -28,17 +28,17 @@
|
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"cookie": "2.0.1",
|
|
30
30
|
"date-fns": "4.4.0",
|
|
31
|
-
"zod": "4.4
|
|
31
|
+
"zod": "4.5.4"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@scaleway/sdk-client": "2.
|
|
35
|
-
"@scaleway/sdk-iam": "2.
|
|
36
|
-
"@types/react": "19.2.
|
|
37
|
-
"react": "19.2.
|
|
34
|
+
"@scaleway/sdk-client": "2.7.0",
|
|
35
|
+
"@scaleway/sdk-iam": "2.15.0",
|
|
36
|
+
"@types/react": "19.2.18",
|
|
37
|
+
"react": "19.2.8"
|
|
38
38
|
},
|
|
39
39
|
"peerDependencies": {
|
|
40
|
-
"@scaleway/sdk-client": "2.
|
|
41
|
-
"@scaleway/sdk-iam": "2.
|
|
40
|
+
"@scaleway/sdk-client": "2.7.0",
|
|
41
|
+
"@scaleway/sdk-iam": "2.15.0",
|
|
42
42
|
"react": ">=18.0"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|