@schemavaults/auth-client-sdk 0.5.0
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 +14 -0
- package/dist/auth-client.d.ts +128 -0
- package/dist/auth-client.js +1190 -0
- package/dist/auth-client.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -0
- package/dist/lib/acquire-access-token.d.ts +11 -0
- package/dist/lib/acquire-access-token.js +126 -0
- package/dist/lib/acquire-access-token.js.map +1 -0
- package/dist/lib/auth-client-events.d.ts +7 -0
- package/dist/lib/auth-client-events.js +4 -0
- package/dist/lib/auth-client-events.js.map +1 -0
- package/dist/lib/authenticate-url-encoder.d.ts +19 -0
- package/dist/lib/authenticate-url-encoder.js +41 -0
- package/dist/lib/authenticate-url-encoder.js.map +1 -0
- package/dist/lib/authentication-outcome-type.d.ts +3 -0
- package/dist/lib/authentication-outcome-type.js +15 -0
- package/dist/lib/authentication-outcome-type.js.map +1 -0
- package/dist/lib/credentials-schema/credentials-schema.d.ts +28 -0
- package/dist/lib/credentials-schema/credentials-schema.js +14 -0
- package/dist/lib/credentials-schema/credentials-schema.js.map +1 -0
- package/dist/lib/credentials-schema/index.d.ts +1 -0
- package/dist/lib/credentials-schema/index.js +2 -0
- package/dist/lib/credentials-schema/index.js.map +1 -0
- package/dist/lib/debugPrintTokensAsTable.d.ts +2 -0
- package/dist/lib/debugPrintTokensAsTable.js +14 -0
- package/dist/lib/debugPrintTokensAsTable.js.map +1 -0
- package/dist/lib/debugPrintUserDataAsTable.d.ts +2 -0
- package/dist/lib/debugPrintUserDataAsTable.js +12 -0
- package/dist/lib/debugPrintUserDataAsTable.js.map +1 -0
- package/dist/lib/is-private-beta.d.ts +3 -0
- package/dist/lib/is-private-beta.js +24 -0
- package/dist/lib/is-private-beta.js.map +1 -0
- package/dist/lib/isValidRefreshToken.d.ts +2 -0
- package/dist/lib/isValidRefreshToken.js +13 -0
- package/dist/lib/isValidRefreshToken.js.map +1 -0
- package/dist/lib/send-authenticate-request.d.ts +2 -0
- package/dist/lib/send-authenticate-request.js +133 -0
- package/dist/lib/send-authenticate-request.js.map +1 -0
- package/dist/types/IAuthClientConstructorOptions.d.ts +14 -0
- package/dist/types/IAuthClientConstructorOptions.js +2 -0
- package/dist/types/IAuthClientConstructorOptions.js.map +1 -0
- package/dist/types/ISchemaVaultsAuthClient.d.ts +59 -0
- package/dist/types/ISchemaVaultsAuthClient.js +2 -0
- package/dist/types/ISchemaVaultsAuthClient.js.map +1 -0
- package/dist/types/ISchemaVaultsAuthClientAdapter.d.ts +62 -0
- package/dist/types/ISchemaVaultsAuthClientAdapter.js +2 -0
- package/dist/types/ISchemaVaultsAuthClientAdapter.js.map +1 -0
- package/dist/types/ISendAuthenticateRequestOptions.d.ts +13 -0
- package/dist/types/ISendAuthenticateRequestOptions.js +2 -0
- package/dist/types/ISendAuthenticateRequestOptions.js.map +1 -0
- package/dist/types/UserData.d.ts +1 -0
- package/dist/types/UserData.js +2 -0
- package/dist/types/UserData.js.map +1 -0
- package/dist/types/acquire-access-token-options.d.ts +8 -0
- package/dist/types/acquire-access-token-options.js +2 -0
- package/dist/types/acquire-access-token-options.js.map +1 -0
- package/dist/types/credentials.d.ts +3 -0
- package/dist/types/credentials.js +2 -0
- package/dist/types/credentials.js.map +1 -0
- package/dist/types/framework-adapter-interface.d.ts +36 -0
- package/dist/types/framework-adapter-interface.js +2 -0
- package/dist/types/framework-adapter-interface.js.map +1 -0
- package/package.json +42 -0
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import isPrivateBetaEnabled from "../lib/is-private-beta";
|
|
2
|
+
import { PKCE_ProofKeyManager, authenticateResultSchema, } from "@schemavaults/auth-common";
|
|
3
|
+
import { credentialsSchema } from "../lib/credentials-schema";
|
|
4
|
+
import { isValidAuthenticationOutcomeType, } from "./authentication-outcome-type";
|
|
5
|
+
const PRIVATE_BETA = isPrivateBetaEnabled();
|
|
6
|
+
// Send an authentication request to the auth server, hopefully get an authorization code back, else throw an error
|
|
7
|
+
export async function sendAuthenticateRequest(opts) {
|
|
8
|
+
const credentials = opts.credentials;
|
|
9
|
+
const code_challenge = opts.code_challenge;
|
|
10
|
+
const authentication_type = opts.authentication_type;
|
|
11
|
+
const adapter = opts.adapter;
|
|
12
|
+
const env = opts.app_environment;
|
|
13
|
+
if (env === "development") {
|
|
14
|
+
console.log("[sendAuthenticateRequest] sending request to the auth server...");
|
|
15
|
+
}
|
|
16
|
+
if (!isValidAuthenticationOutcomeType(authentication_type)) {
|
|
17
|
+
throw new Error("Invalid authentication outcome type");
|
|
18
|
+
}
|
|
19
|
+
const parsed_credentials = credentialsSchema.safeParse(credentials);
|
|
20
|
+
if (!parsed_credentials.success) {
|
|
21
|
+
console.error(parsed_credentials.error);
|
|
22
|
+
throw new Error("Invalid credentials");
|
|
23
|
+
}
|
|
24
|
+
const parsed_code_challenge = PKCE_ProofKeyManager.codeChallengeSchema.safeParse(code_challenge.code_challenge);
|
|
25
|
+
if (!parsed_code_challenge.success) {
|
|
26
|
+
console.error(parsed_code_challenge.error);
|
|
27
|
+
throw new Error("Invalid code challenge");
|
|
28
|
+
}
|
|
29
|
+
if (code_challenge.code_challenge_method !== "S256") {
|
|
30
|
+
throw new Error("Invalid code challenge method");
|
|
31
|
+
}
|
|
32
|
+
if (authentication_type === "reset-password") {
|
|
33
|
+
throw new Error("Not implemented");
|
|
34
|
+
}
|
|
35
|
+
if (!credentials.email) {
|
|
36
|
+
throw new Error("Email is required");
|
|
37
|
+
}
|
|
38
|
+
if (authentication_type === "login" && !credentials.password) {
|
|
39
|
+
throw new Error("Password is required");
|
|
40
|
+
}
|
|
41
|
+
if (authentication_type === "register") {
|
|
42
|
+
if (!credentials.password) {
|
|
43
|
+
throw new Error("Password is required");
|
|
44
|
+
}
|
|
45
|
+
if (!credentials.confirm) {
|
|
46
|
+
throw new Error("Password confirmation is required");
|
|
47
|
+
}
|
|
48
|
+
if (credentials.password !== credentials.confirm) {
|
|
49
|
+
throw new Error("Passwords do not match");
|
|
50
|
+
}
|
|
51
|
+
if (PRIVATE_BETA && !credentials.invite_code) {
|
|
52
|
+
throw new Error("Invite code is required while in private beta");
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
const auth_request_body = {
|
|
56
|
+
credentials: {
|
|
57
|
+
email: credentials.email,
|
|
58
|
+
password: credentials.password,
|
|
59
|
+
},
|
|
60
|
+
invite_code: credentials.invite_code,
|
|
61
|
+
code_challenge: code_challenge.code_challenge,
|
|
62
|
+
challenge_time: code_challenge.challenge_time,
|
|
63
|
+
};
|
|
64
|
+
let response;
|
|
65
|
+
try {
|
|
66
|
+
if (env === "development") {
|
|
67
|
+
console.log("[sendAuthenticateRequest] Sending POST request via adapter");
|
|
68
|
+
}
|
|
69
|
+
const authentication_request_response = await adapter.sendPOSTRequest(`/api/auth/${authentication_type}`, auth_request_body, {});
|
|
70
|
+
if (!authentication_request_response) {
|
|
71
|
+
throw new Error("No response received from client auth adapter HTTP client");
|
|
72
|
+
}
|
|
73
|
+
response = authentication_request_response;
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
console.error("Failed to send HTTP authentication request: ", e);
|
|
77
|
+
throw new Error("Failed to send HTTP authentication request");
|
|
78
|
+
}
|
|
79
|
+
if (typeof response.status === "number" && response.status >= 500) {
|
|
80
|
+
let errorMsg = "Unknown server-side error handling authentication request :(";
|
|
81
|
+
try {
|
|
82
|
+
if (typeof response.data === "object" && response.data !== null) {
|
|
83
|
+
if ("message" in response.data &&
|
|
84
|
+
typeof response.data.message === "string") {
|
|
85
|
+
errorMsg = response.data.message;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
catch (error) {
|
|
90
|
+
void error;
|
|
91
|
+
}
|
|
92
|
+
throw new Error(errorMsg);
|
|
93
|
+
}
|
|
94
|
+
if (typeof response.status === "number" && response.status === 404) {
|
|
95
|
+
throw new Error("User does not exist! Ensure that you have the correct credentials!");
|
|
96
|
+
}
|
|
97
|
+
if (typeof response.status === "number" && response.status === 409) {
|
|
98
|
+
throw new Error("User already exists! Try logging in.");
|
|
99
|
+
}
|
|
100
|
+
try {
|
|
101
|
+
if (!response.ok) {
|
|
102
|
+
if (response.status === 401) {
|
|
103
|
+
throw new Error("Invalid credentials");
|
|
104
|
+
}
|
|
105
|
+
else if (response.status === 404 && authentication_type === "login") {
|
|
106
|
+
throw new Error("User does not exist! Ensure that you have the correct credentials!");
|
|
107
|
+
}
|
|
108
|
+
throw new Error("Failed to authenticate");
|
|
109
|
+
}
|
|
110
|
+
const response_body_json = response.data;
|
|
111
|
+
const parsed_auth_response = await authenticateResultSchema.safeParseAsync(response_body_json);
|
|
112
|
+
if (!parsed_auth_response.success) {
|
|
113
|
+
throw new Error(parsed_auth_response.error.errors.join(", "));
|
|
114
|
+
}
|
|
115
|
+
const data = parsed_auth_response.data;
|
|
116
|
+
if (!data.success) {
|
|
117
|
+
throw new Error(data.message);
|
|
118
|
+
}
|
|
119
|
+
const authorization_code = data.authorization_code;
|
|
120
|
+
if (typeof authorization_code !== "string") {
|
|
121
|
+
throw new Error("Invalid authorization code");
|
|
122
|
+
}
|
|
123
|
+
return authorization_code;
|
|
124
|
+
}
|
|
125
|
+
catch (e) {
|
|
126
|
+
if (e instanceof Error && e.message.includes("Invalid credentials")) {
|
|
127
|
+
throw new Error("Invalid credentials");
|
|
128
|
+
}
|
|
129
|
+
console.error("Failed to parse authentication response: ", e);
|
|
130
|
+
throw new Error("Failed to parse authentication response");
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
//# sourceMappingURL=send-authenticate-request.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"send-authenticate-request.js","sourceRoot":"","sources":["../../src/lib/send-authenticate-request.ts"],"names":[],"mappings":"AAAA,OAAO,oBAAoB,MAAM,uBAAuB,CAAC;AACzD,OAAO,EAEL,oBAAoB,EACpB,wBAAwB,GACzB,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AE,CAAC;YAC3C,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;QAChD,CAAC;QAED,OAAO,kBAAmC,CAAC;IAC7C,CAAC;IAAC,OAAO,CAAU,EAAE,CAAC;QACpB,IAAI,CAAC,YAAY,KAAK,IAAI,CAAC,CAAC,OAAO,CAAC,QAAQ,CAAC,qBAAqB,CAAC,EAAE,CAAC;YACpE,MAAM,IAAI,KAAK,CAAC,qBAAqB,CAAC,CAAC;QACzC,CAAC;QACD,OAAO,CAAC,KAAK,CAAC,2CAA2C,EAAE,CAAC,CAAC,CAAC;QAC9D,MAAM,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;IAC7D,CAAC;AACH,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { SchemaVaultsAppEnvironment } from "@schemavaults/app-definitions";
|
|
2
|
+
import type { ISchemaVaultsAuthClientAdapter } from "./ISchemaVaultsAuthClientAdapter";
|
|
3
|
+
export interface IAuthClientConstructorOptions {
|
|
4
|
+
adapter: ISchemaVaultsAuthClientAdapter;
|
|
5
|
+
auth_server_uri: string;
|
|
6
|
+
successful_authentication_redirect_uri: string;
|
|
7
|
+
successful_logout_redirect_uri?: string;
|
|
8
|
+
authorize_uri?: string;
|
|
9
|
+
app_id: string;
|
|
10
|
+
default_audiences?: string[];
|
|
11
|
+
debug?: boolean;
|
|
12
|
+
app_env: SchemaVaultsAppEnvironment;
|
|
13
|
+
}
|
|
14
|
+
export type { IAuthClientConstructorOptions as InitializeAuthClientOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"IAuthClientConstructorOptions.js","sourceRoot":"","sources":["../../src/types/IAuthClientConstructorOptions.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { AccessToken, RefreshToken, UserData, CodeChallengeWithDetails } from "@schemavaults/auth-common";
|
|
2
|
+
import type { Credentials } from "../types/credentials";
|
|
3
|
+
import type { AuthenticationOutcomeType } from "../lib/authentication-outcome-type";
|
|
4
|
+
import type { AcquireAccessTokenOptions } from "../types/acquire-access-token-options";
|
|
5
|
+
import type { AppId } from "@schemavaults/app-definitions";
|
|
6
|
+
export interface ISchemaVaultsAuthClient {
|
|
7
|
+
app_id: AppId;
|
|
8
|
+
auth_server_uri: string;
|
|
9
|
+
login: () => Promise<void>;
|
|
10
|
+
register: () => Promise<void>;
|
|
11
|
+
successful_logout_redirect_uri: string | undefined;
|
|
12
|
+
logout: () => Promise<void>;
|
|
13
|
+
generateCodeChallenge: () => Promise<CodeChallengeWithDetails>;
|
|
14
|
+
sendAuthenticateRequest: (authentication_type: AuthenticationOutcomeType, credentials: Credentials, code_challenge: CodeChallengeWithDetails) => Promise<string>;
|
|
15
|
+
successful_authentication_redirect_uri: string;
|
|
16
|
+
authorize_uri: string | undefined;
|
|
17
|
+
handleSuccessfulAuthentication: (authorization_code: string, challenge_time: number, code_verifier?: string) => Promise<void>;
|
|
18
|
+
getAccessTokenFromCache: (token_id: string) => AccessToken | null;
|
|
19
|
+
getRefreshTokenFromCache: () => RefreshToken | null;
|
|
20
|
+
hasHttpOnlyRefreshToken: () => boolean;
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param opts Options for the type & how the access token should be retrieved
|
|
24
|
+
* @param ensure_fresh Make sure this access token is acquired from the auth server "fresh"; don't use one from the cache (if it exists)
|
|
25
|
+
* @returns AccessToken
|
|
26
|
+
*/
|
|
27
|
+
acquireAccessToken: (opts: AcquireAccessTokenOptions) => Promise<AccessToken>;
|
|
28
|
+
secure: boolean;
|
|
29
|
+
currentUser: UserData | null;
|
|
30
|
+
/**
|
|
31
|
+
* @name onAuthStateChanged
|
|
32
|
+
* @param listener A callback that is called whenever an auth state change event is emitted
|
|
33
|
+
* @param listener_id A unique ID for this listener, to allow it to be removed. A uuid is generated if an ID is not supplied.
|
|
34
|
+
* @returns The listener_id that the callback was registered with
|
|
35
|
+
*/
|
|
36
|
+
onAuthStateChanged: (listener: () => void, listener_id?: string) => string;
|
|
37
|
+
/**
|
|
38
|
+
* @name removeAuthStateChangeListener
|
|
39
|
+
* @param listener_id A unique ID for the listener callback to remove
|
|
40
|
+
* @returns None
|
|
41
|
+
* @throws if no callback with listener_id exists, or if it was unable to be deleted
|
|
42
|
+
*/
|
|
43
|
+
removeAuthStateChangeListener: (listener_id: string) => void;
|
|
44
|
+
/**
|
|
45
|
+
* @name loadSavedAuthorizationCodeVerifiers
|
|
46
|
+
* @returns Code verifiers saved by auth adapter
|
|
47
|
+
*/
|
|
48
|
+
loadSavedAuthorizationCodeVerifiers: () => Promise<Record<number, string>>;
|
|
49
|
+
/**
|
|
50
|
+
* @name isAuthenticated
|
|
51
|
+
* @returns Getter that returns true if there is a user currently signed in, false otherwise
|
|
52
|
+
*/
|
|
53
|
+
isAuthenticated: boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Return true if feature is supported by this auth client / adapter
|
|
56
|
+
* @param feature_name A feature name, e.g. 'http-only-refresh-token'
|
|
57
|
+
*/
|
|
58
|
+
supports(feature_name: string): boolean;
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ISchemaVaultsAuthClient.js","sourceRoot":"","sources":["../../src/types/ISchemaVaultsAuthClient.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { AccessToken, RefreshToken, UserData } from "@schemavaults/auth-common";
|
|
2
|
+
interface AuthClientCodeVerifierActions {
|
|
3
|
+
storeCodeVerifier: (codeVerifier: string, challenge_time: number) => void;
|
|
4
|
+
loadCodeVerifier: (challenge_time: number) => string | null;
|
|
5
|
+
loadCodeVerifiers: () => Record<number, string>;
|
|
6
|
+
clearCodeVerifiers: () => void;
|
|
7
|
+
clearCodeVerifier: (challenge_time: number) => void;
|
|
8
|
+
}
|
|
9
|
+
interface AuthClientUserDataActions {
|
|
10
|
+
storeUserData: (userData: UserData) => void;
|
|
11
|
+
getUserData: () => UserData | null;
|
|
12
|
+
clearUserData: () => void;
|
|
13
|
+
}
|
|
14
|
+
interface AuthClientAuthTokensActions {
|
|
15
|
+
storeRefreshToken: (refresh_token: RefreshToken) => void;
|
|
16
|
+
storeAccessToken: (token_id: string, access_token: AccessToken) => void;
|
|
17
|
+
doesSupportHttpOnlyRefreshToken?: undefined | (() => boolean);
|
|
18
|
+
clearHttpOnlyRefreshToken?: () => Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* @name hasHttpOnlyRefreshToken
|
|
21
|
+
* @returns True if an HTTP-only refresh token cookie has been marked as received
|
|
22
|
+
*/
|
|
23
|
+
hasHttpOnlyRefreshToken?: undefined | (() => boolean);
|
|
24
|
+
/**
|
|
25
|
+
* @name hasRefreshToken
|
|
26
|
+
* @returns True if there is either:
|
|
27
|
+
* 1.) a RefreshToken stored locally, or
|
|
28
|
+
* 2.) an HTTP-only refresh token cookie has been marked as received
|
|
29
|
+
*/
|
|
30
|
+
hasRefreshToken: () => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* @name getRefreshToken
|
|
33
|
+
* @description Loads a RefreshToken stored locally.
|
|
34
|
+
* However, it's possible that we have an 'HTTP-only refresh token cookie' received-- in this case, null is returned here.
|
|
35
|
+
* @returns a RefreshToken stored locally, or null if one is not found (that is accessible to JS).
|
|
36
|
+
* @see hasRefreshToken, doesSupportHttpOnlyRefreshToken
|
|
37
|
+
*/
|
|
38
|
+
getRefreshToken: () => RefreshToken | null;
|
|
39
|
+
/**
|
|
40
|
+
* @name getAccessToken
|
|
41
|
+
* @argument token_id - The ID of the token to retrieve (usually the desired server audience)
|
|
42
|
+
* @description Loads an AccessToken stored locally.
|
|
43
|
+
* @returns an AccessToken stored locally, or null if one is not found
|
|
44
|
+
*/
|
|
45
|
+
getAccessToken: (token_id: string) => AccessToken | null;
|
|
46
|
+
clearAuthTokens: () => Promise<void>;
|
|
47
|
+
clearAccessToken: (token_id: string) => void;
|
|
48
|
+
clearAccessTokens: () => void;
|
|
49
|
+
}
|
|
50
|
+
export interface IAuthClientPOSTResultType<T extends object> {
|
|
51
|
+
status: number;
|
|
52
|
+
ok: boolean;
|
|
53
|
+
data: T;
|
|
54
|
+
}
|
|
55
|
+
interface AuthClientNetworkActions {
|
|
56
|
+
sendPOSTRequest: (url: string, body: Record<string, unknown>, headers: Record<string, string>) => Promise<IAuthClientPOSTResultType<object>>;
|
|
57
|
+
}
|
|
58
|
+
export interface ISchemaVaultsAuthClientAdapter extends AuthClientCodeVerifierActions, AuthClientUserDataActions, AuthClientAuthTokensActions, AuthClientNetworkActions {
|
|
59
|
+
redirect: (uri: string) => void | Promise<void>;
|
|
60
|
+
uuid: () => string;
|
|
61
|
+
}
|
|
62
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ISchemaVaultsAuthClientAdapter.js","sourceRoot":"","sources":["../../src/types/ISchemaVaultsAuthClientAdapter.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { AuthenticationOutcomeType } from "../lib/authentication-outcome-type";
|
|
2
|
+
import type { ISchemaVaultsAuthClientAdapter } from "../types/ISchemaVaultsAuthClientAdapter";
|
|
3
|
+
import type { Credentials } from "./credentials";
|
|
4
|
+
import type { CodeChallengeWithDetails } from "@schemavaults/auth-common";
|
|
5
|
+
import type { SchemaVaultsAppEnvironment } from "@schemavaults/app-definitions";
|
|
6
|
+
export interface ISendAuthenticateRequestOptions {
|
|
7
|
+
adapter: ISchemaVaultsAuthClientAdapter;
|
|
8
|
+
authentication_type: AuthenticationOutcomeType;
|
|
9
|
+
credentials: Credentials;
|
|
10
|
+
code_challenge: CodeChallengeWithDetails;
|
|
11
|
+
app_environment: SchemaVaultsAppEnvironment;
|
|
12
|
+
}
|
|
13
|
+
export type { ISendAuthenticateRequestOptions as SendAuthenticateRequestOptions };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ISendAuthenticateRequestOptions.js","sourceRoot":"","sources":["../../src/types/ISendAuthenticateRequestOptions.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export type { UserData } from "@schemavaults/auth-common";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UserData.js","sourceRoot":"","sources":["../../src/types/UserData.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"acquire-access-token-options.js","sourceRoot":"","sources":["../../src/types/acquire-access-token-options.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credentials.js","sourceRoot":"","sources":["../../src/types/credentials.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { AccessToken, RefreshToken, UserData } from "@schemavaults/auth-common";
|
|
2
|
+
interface AuthClientCodeVerifierActions {
|
|
3
|
+
storeCodeVerifier: (codeVerifier: string, challenge_time: number) => void;
|
|
4
|
+
loadCodeVerifier: (challenge_time: number) => string | null;
|
|
5
|
+
loadCodeVerifiers: () => Record<number, string>;
|
|
6
|
+
clearCodeVerifiers: () => void;
|
|
7
|
+
clearCodeVerifier: (challenge_time: number) => void;
|
|
8
|
+
}
|
|
9
|
+
interface AuthClientUserDataActions {
|
|
10
|
+
storeUserData: (userData: UserData) => void;
|
|
11
|
+
getUserData: () => UserData | null;
|
|
12
|
+
clearUserData: () => void;
|
|
13
|
+
}
|
|
14
|
+
interface AuthClientAuthTokensActions {
|
|
15
|
+
storeRefreshToken: (refresh_token: RefreshToken) => void;
|
|
16
|
+
storeAccessToken: (token_id: string, access_token: AccessToken) => void;
|
|
17
|
+
setHttpOnlyRefreshTokenReceived?: undefined | (() => void);
|
|
18
|
+
doesSupportHttpOnlyRefreshToken?: undefined | (() => boolean);
|
|
19
|
+
getRefreshToken: () => RefreshToken | null;
|
|
20
|
+
getAccessToken: (token_id: string) => AccessToken | null;
|
|
21
|
+
clearAuthTokens: () => void;
|
|
22
|
+
clearAccessToken: (token_id: string) => void;
|
|
23
|
+
}
|
|
24
|
+
export interface IAuthClientPOSTResultType<T extends object> {
|
|
25
|
+
status: number;
|
|
26
|
+
ok: boolean;
|
|
27
|
+
data: T;
|
|
28
|
+
}
|
|
29
|
+
interface AuthClientNetworkActions {
|
|
30
|
+
sendPOSTRequest: (url: string, body: Record<string, unknown>, headers: Record<string, string>) => Promise<IAuthClientPOSTResultType<object>>;
|
|
31
|
+
}
|
|
32
|
+
export interface ISchemaVaultsAuthClientAdapter extends AuthClientCodeVerifierActions, AuthClientUserDataActions, AuthClientAuthTokensActions, AuthClientNetworkActions {
|
|
33
|
+
redirect: (uri: string) => void | Promise<void>;
|
|
34
|
+
uuid: () => string;
|
|
35
|
+
}
|
|
36
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"framework-adapter-interface.js","sourceRoot":"","sources":["../../src/types/framework-adapter-interface.ts"],"names":[],"mappings":""}
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@schemavaults/auth-client-sdk",
|
|
3
|
+
"description": "TypeScript SDK for interacting with SchemaVaults Auth server or protected resources",
|
|
4
|
+
"version": "0.5.0",
|
|
5
|
+
"license": "UNLICENSED",
|
|
6
|
+
"private": false,
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/schemavaults/auth.git",
|
|
10
|
+
"directory": "packages/auth-client-sdk"
|
|
11
|
+
},
|
|
12
|
+
"type": "module",
|
|
13
|
+
"main": "dist/index.js",
|
|
14
|
+
"types": "dist/index.d.ts",
|
|
15
|
+
"dependencies": {
|
|
16
|
+
"zod": "3.23.8",
|
|
17
|
+
"@schemavaults/auth-common": "0.7.27",
|
|
18
|
+
"@schemavaults/app-definitions": "0.6.1"
|
|
19
|
+
},
|
|
20
|
+
"scripts": {
|
|
21
|
+
"build": "tsc --project tsconfig.json && tsc-alias --project tsconfig.json",
|
|
22
|
+
"postbuild": "bun run cleanup",
|
|
23
|
+
"test": "NODE_ENV=test bun test",
|
|
24
|
+
"cleanup:compiled_tests_in_dist_directory": "find ./dist -type f \\( -name \"*.test.js\" -o -name \"*.test.js.map\" -o -name \"*.test.d.ts\" \\) -delete",
|
|
25
|
+
"cleanup": "bun run cleanup:compiled_tests_in_dist_directory",
|
|
26
|
+
"lint": "eslint src --ext .ts,.tsx"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"typescript": "5.9.3",
|
|
30
|
+
"bun-types": "1.3.6",
|
|
31
|
+
"tsc-alias": "1.8.16",
|
|
32
|
+
"eslint": "9.39.1",
|
|
33
|
+
"@eslint/js": "9.39.1",
|
|
34
|
+
"globals": "16.5.0",
|
|
35
|
+
"@typescript-eslint/eslint-plugin": "8.48.1",
|
|
36
|
+
"@typescript-eslint/parser": "8.48.1"
|
|
37
|
+
},
|
|
38
|
+
"publishConfig": {
|
|
39
|
+
"access": "public"
|
|
40
|
+
},
|
|
41
|
+
"packageManager": "bun@1.3.6"
|
|
42
|
+
}
|