@idealyst/oauth-client 1.0.98 → 1.1.1
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/dist/index.d.mts +25 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +92 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +69 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +2 -2
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
interface OAuthConfig {
|
|
2
|
+
oauthUrl: string;
|
|
3
|
+
redirectUrl: string;
|
|
4
|
+
additionalParameters?: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
interface OAuthResult {
|
|
7
|
+
code: string;
|
|
8
|
+
state?: string;
|
|
9
|
+
}
|
|
10
|
+
interface OAuthClient {
|
|
11
|
+
authorize(): Promise<OAuthResult>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare class WebOAuthClient implements OAuthClient {
|
|
15
|
+
private config;
|
|
16
|
+
constructor(config: OAuthConfig);
|
|
17
|
+
authorize(): Promise<OAuthResult>;
|
|
18
|
+
private checkForCallback;
|
|
19
|
+
private buildOAuthUrl;
|
|
20
|
+
private generateState;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare function createOAuthClient(config: OAuthConfig): OAuthClient;
|
|
24
|
+
|
|
25
|
+
export { type OAuthClient, type OAuthConfig, type OAuthResult, WebOAuthClient, createOAuthClient };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
interface OAuthConfig {
|
|
2
|
+
oauthUrl: string;
|
|
3
|
+
redirectUrl: string;
|
|
4
|
+
additionalParameters?: Record<string, string>;
|
|
5
|
+
}
|
|
6
|
+
interface OAuthResult {
|
|
7
|
+
code: string;
|
|
8
|
+
state?: string;
|
|
9
|
+
}
|
|
10
|
+
interface OAuthClient {
|
|
11
|
+
authorize(): Promise<OAuthResult>;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
declare class WebOAuthClient implements OAuthClient {
|
|
15
|
+
private config;
|
|
16
|
+
constructor(config: OAuthConfig);
|
|
17
|
+
authorize(): Promise<OAuthResult>;
|
|
18
|
+
private checkForCallback;
|
|
19
|
+
private buildOAuthUrl;
|
|
20
|
+
private generateState;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
declare function createOAuthClient(config: OAuthConfig): OAuthClient;
|
|
24
|
+
|
|
25
|
+
export { type OAuthClient, type OAuthConfig, type OAuthResult, WebOAuthClient, createOAuthClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
WebOAuthClient: () => WebOAuthClient,
|
|
24
|
+
createOAuthClient: () => createOAuthClient
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/oauth-client.web.ts
|
|
29
|
+
var WebOAuthClient = class {
|
|
30
|
+
constructor(config) {
|
|
31
|
+
this.config = config;
|
|
32
|
+
}
|
|
33
|
+
async authorize() {
|
|
34
|
+
const state = this.generateState();
|
|
35
|
+
const callbackData = this.checkForCallback();
|
|
36
|
+
if (callbackData) {
|
|
37
|
+
const { code, error, returnedState } = callbackData;
|
|
38
|
+
if (error) {
|
|
39
|
+
throw new Error(`OAuth error: ${error}`);
|
|
40
|
+
}
|
|
41
|
+
if (code) {
|
|
42
|
+
window.history.replaceState({}, document.title, window.location.pathname);
|
|
43
|
+
return {
|
|
44
|
+
code,
|
|
45
|
+
state: returnedState || void 0
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
const oauthUrl = this.buildOAuthUrl(state);
|
|
50
|
+
window.location.href = oauthUrl;
|
|
51
|
+
throw new Error("Authorization flow initiated");
|
|
52
|
+
}
|
|
53
|
+
checkForCallback() {
|
|
54
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
55
|
+
const code = urlParams.get("code");
|
|
56
|
+
const error = urlParams.get("error");
|
|
57
|
+
const state = urlParams.get("state");
|
|
58
|
+
if (code || error) {
|
|
59
|
+
return {
|
|
60
|
+
code: code || void 0,
|
|
61
|
+
error: error || void 0,
|
|
62
|
+
returnedState: state || void 0
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
buildOAuthUrl(state) {
|
|
68
|
+
const url = new URL(this.config.oauthUrl);
|
|
69
|
+
url.searchParams.set("redirect_uri", this.config.redirectUrl);
|
|
70
|
+
url.searchParams.set("state", state);
|
|
71
|
+
if (this.config.additionalParameters) {
|
|
72
|
+
Object.entries(this.config.additionalParameters).forEach(([key, value]) => {
|
|
73
|
+
url.searchParams.set(key, value);
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return url.toString();
|
|
77
|
+
}
|
|
78
|
+
generateState() {
|
|
79
|
+
let result = "";
|
|
80
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
81
|
+
for (let i = 0; i < 32; i++) {
|
|
82
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
83
|
+
}
|
|
84
|
+
return result;
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// src/index.web.ts
|
|
89
|
+
function createOAuthClient(config) {
|
|
90
|
+
return new WebOAuthClient(config);
|
|
91
|
+
}
|
|
92
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/oauth-client.web.ts","../src/index.web.ts"],"sourcesContent":["export * from './index.web'","import type { OAuthClient, OAuthConfig, OAuthResult } from './types'\n\nexport class WebOAuthClient implements OAuthClient {\n private config: OAuthConfig\n\n constructor(config: OAuthConfig) {\n this.config = config\n }\n\n async authorize(): Promise<OAuthResult> {\n const state = this.generateState()\n\n // Check if we're already in a callback\n const callbackData = this.checkForCallback()\n \n if (callbackData) {\n const { code, error, returnedState } = callbackData\n \n if (error) {\n throw new Error(`OAuth error: ${error}`)\n }\n\n if (code) {\n // Clean up URL\n window.history.replaceState({}, document.title, window.location.pathname)\n \n return { \n code,\n state: returnedState || undefined\n }\n }\n }\n\n // Build OAuth URL and redirect\n const oauthUrl = this.buildOAuthUrl(state)\n window.location.href = oauthUrl\n \n // This won't be reached due to redirect\n throw new Error('Authorization flow initiated')\n }\n\n private checkForCallback(): { code?: string; error?: string; returnedState?: string } | null {\n const urlParams = new URLSearchParams(window.location.search)\n const code = urlParams.get('code')\n const error = urlParams.get('error')\n const state = urlParams.get('state')\n \n if (code || error) {\n return {\n code: code || undefined,\n error: error || undefined,\n returnedState: state || undefined,\n }\n }\n \n return null\n }\n\n private buildOAuthUrl(state: string): string {\n const url = new URL(this.config.oauthUrl)\n \n url.searchParams.set('redirect_uri', this.config.redirectUrl)\n url.searchParams.set('state', state)\n\n // Add additional parameters\n if (this.config.additionalParameters) {\n Object.entries(this.config.additionalParameters).forEach(([key, value]) => {\n url.searchParams.set(key, value)\n })\n }\n\n return url.toString()\n }\n\n private generateState(): string {\n // Generate random state for CSRF protection\n let result = ''\n const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'\n for (let i = 0; i < 32; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length))\n }\n return result\n }\n}","export * from './types'\n\nimport type { OAuthConfig, OAuthClient } from './types'\nimport { WebOAuthClient } from './oauth-client.web'\n\nexport function createOAuthClient(config: OAuthConfig): OAuthClient {\n return new WebOAuthClient(config)\n}\n\nexport { WebOAuthClient }"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACEO,IAAM,iBAAN,MAA4C;AAAA,EAGjD,YAAY,QAAqB;AAC/B,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,MAAM,YAAkC;AACtC,UAAM,QAAQ,KAAK,cAAc;AAGjC,UAAM,eAAe,KAAK,iBAAiB;AAE3C,QAAI,cAAc;AAChB,YAAM,EAAE,MAAM,OAAO,cAAc,IAAI;AAEvC,UAAI,OAAO;AACT,cAAM,IAAI,MAAM,gBAAgB,KAAK,EAAE;AAAA,MACzC;AAEA,UAAI,MAAM;AAER,eAAO,QAAQ,aAAa,CAAC,GAAG,SAAS,OAAO,OAAO,SAAS,QAAQ;AAExE,eAAO;AAAA,UACL;AAAA,UACA,OAAO,iBAAiB;AAAA,QAC1B;AAAA,MACF;AAAA,IACF;AAGA,UAAM,WAAW,KAAK,cAAc,KAAK;AACzC,WAAO,SAAS,OAAO;AAGvB,UAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD;AAAA,EAEQ,mBAAqF;AAC3F,UAAM,YAAY,IAAI,gBAAgB,OAAO,SAAS,MAAM;AAC5D,UAAM,OAAO,UAAU,IAAI,MAAM;AACjC,UAAM,QAAQ,UAAU,IAAI,OAAO;AACnC,UAAM,QAAQ,UAAU,IAAI,OAAO;AAEnC,QAAI,QAAQ,OAAO;AACjB,aAAO;AAAA,QACL,MAAM,QAAQ;AAAA,QACd,OAAO,SAAS;AAAA,QAChB,eAAe,SAAS;AAAA,MAC1B;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,cAAc,OAAuB;AAC3C,UAAM,MAAM,IAAI,IAAI,KAAK,OAAO,QAAQ;AAExC,QAAI,aAAa,IAAI,gBAAgB,KAAK,OAAO,WAAW;AAC5D,QAAI,aAAa,IAAI,SAAS,KAAK;AAGnC,QAAI,KAAK,OAAO,sBAAsB;AACpC,aAAO,QAAQ,KAAK,OAAO,oBAAoB,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACzE,YAAI,aAAa,IAAI,KAAK,KAAK;AAAA,MACjC,CAAC;AAAA,IACH;AAEA,WAAO,IAAI,SAAS;AAAA,EACtB;AAAA,EAEQ,gBAAwB;AAE9B,QAAI,SAAS;AACb,UAAM,QAAQ;AACd,aAAS,IAAI,GAAG,IAAI,IAAI,KAAK;AAC3B,gBAAU,MAAM,OAAO,KAAK,MAAM,KAAK,OAAO,IAAI,MAAM,MAAM,CAAC;AAAA,IACjE;AACA,WAAO;AAAA,EACT;AACF;;;AC9EO,SAAS,kBAAkB,QAAkC;AAClE,SAAO,IAAI,eAAe,MAAM;AAClC;","names":[]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// src/oauth-client.web.ts
|
|
2
|
+
var WebOAuthClient = class {
|
|
3
|
+
constructor(config) {
|
|
4
|
+
this.config = config;
|
|
5
|
+
}
|
|
6
|
+
async authorize() {
|
|
7
|
+
const state = this.generateState();
|
|
8
|
+
const callbackData = this.checkForCallback();
|
|
9
|
+
if (callbackData) {
|
|
10
|
+
const { code, error, returnedState } = callbackData;
|
|
11
|
+
if (error) {
|
|
12
|
+
throw new Error(`OAuth error: ${error}`);
|
|
13
|
+
}
|
|
14
|
+
if (code) {
|
|
15
|
+
window.history.replaceState({}, document.title, window.location.pathname);
|
|
16
|
+
return {
|
|
17
|
+
code,
|
|
18
|
+
state: returnedState || void 0
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
const oauthUrl = this.buildOAuthUrl(state);
|
|
23
|
+
window.location.href = oauthUrl;
|
|
24
|
+
throw new Error("Authorization flow initiated");
|
|
25
|
+
}
|
|
26
|
+
checkForCallback() {
|
|
27
|
+
const urlParams = new URLSearchParams(window.location.search);
|
|
28
|
+
const code = urlParams.get("code");
|
|
29
|
+
const error = urlParams.get("error");
|
|
30
|
+
const state = urlParams.get("state");
|
|
31
|
+
if (code || error) {
|
|
32
|
+
return {
|
|
33
|
+
code: code || void 0,
|
|
34
|
+
error: error || void 0,
|
|
35
|
+
returnedState: state || void 0
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
buildOAuthUrl(state) {
|
|
41
|
+
const url = new URL(this.config.oauthUrl);
|
|
42
|
+
url.searchParams.set("redirect_uri", this.config.redirectUrl);
|
|
43
|
+
url.searchParams.set("state", state);
|
|
44
|
+
if (this.config.additionalParameters) {
|
|
45
|
+
Object.entries(this.config.additionalParameters).forEach(([key, value]) => {
|
|
46
|
+
url.searchParams.set(key, value);
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
return url.toString();
|
|
50
|
+
}
|
|
51
|
+
generateState() {
|
|
52
|
+
let result = "";
|
|
53
|
+
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
54
|
+
for (let i = 0; i < 32; i++) {
|
|
55
|
+
result += chars.charAt(Math.floor(Math.random() * chars.length));
|
|
56
|
+
}
|
|
57
|
+
return result;
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/index.web.ts
|
|
62
|
+
function createOAuthClient(config) {
|
|
63
|
+
return new WebOAuthClient(config);
|
|
64
|
+
}
|
|
65
|
+
export {
|
|
66
|
+
WebOAuthClient,
|
|
67
|
+
createOAuthClient
|
|
68
|
+
};
|
|
69
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/oauth-client.web.ts","../src/index.web.ts"],"sourcesContent":["import type { OAuthClient, OAuthConfig, OAuthResult } from './types'\n\nexport class WebOAuthClient implements OAuthClient {\n private config: OAuthConfig\n\n constructor(config: OAuthConfig) {\n this.config = config\n }\n\n async authorize(): Promise<OAuthResult> {\n const state = this.generateState()\n\n // Check if we're already in a callback\n const callbackData = this.checkForCallback()\n \n if (callbackData) {\n const { code, error, returnedState } = callbackData\n \n if (error) {\n throw new Error(`OAuth error: ${error}`)\n }\n\n if (code) {\n // Clean up URL\n window.history.replaceState({}, document.title, window.location.pathname)\n \n return { \n code,\n state: returnedState || undefined\n }\n }\n }\n\n // Build OAuth URL and redirect\n const oauthUrl = this.buildOAuthUrl(state)\n window.location.href = oauthUrl\n \n // This won't be reached due to redirect\n throw new Error('Authorization flow initiated')\n }\n\n private checkForCallback(): { code?: string; error?: string; returnedState?: string } | null {\n const urlParams = new URLSearchParams(window.location.search)\n const code = urlParams.get('code')\n const error = urlParams.get('error')\n const state = urlParams.get('state')\n \n if (code || error) {\n return {\n code: code || undefined,\n error: error || undefined,\n returnedState: state || undefined,\n }\n }\n \n return null\n }\n\n private buildOAuthUrl(state: string): string {\n const url = new URL(this.config.oauthUrl)\n \n url.searchParams.set('redirect_uri', this.config.redirectUrl)\n url.searchParams.set('state', state)\n\n // Add additional parameters\n if (this.config.additionalParameters) {\n Object.entries(this.config.additionalParameters).forEach(([key, value]) => {\n url.searchParams.set(key, value)\n })\n }\n\n return url.toString()\n }\n\n private generateState(): string {\n // Generate random state for CSRF protection\n let result = ''\n const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~'\n for (let i = 0; i < 32; i++) {\n result += chars.charAt(Math.floor(Math.random() * chars.length))\n }\n return result\n }\n}","export * from './types'\n\nimport type { OAuthConfig, OAuthClient } from './types'\nimport { WebOAuthClient } from './oauth-client.web'\n\nexport function createOAuthClient(config: OAuthConfig): OAuthClient {\n return new WebOAuthClient(config)\n}\n\nexport { WebOAuthClient }"],"mappings":";AAEO,IAAM,iBAAN,MAA4C;AAAA,EAGjD,YAAY,QAAqB;AAC/B,SAAK,SAAS;AAAA,EAChB;AAAA,EAEA,MAAM,YAAkC;AACtC,UAAM,QAAQ,KAAK,cAAc;AAGjC,UAAM,eAAe,KAAK,iBAAiB;AAE3C,QAAI,cAAc;AAChB,YAAM,EAAE,MAAM,OAAO,cAAc,IAAI;AAEvC,UAAI,OAAO;AACT,cAAM,IAAI,MAAM,gBAAgB,KAAK,EAAE;AAAA,MACzC;AAEA,UAAI,MAAM;AAER,eAAO,QAAQ,aAAa,CAAC,GAAG,SAAS,OAAO,OAAO,SAAS,QAAQ;AAExE,eAAO;AAAA,UACL;AAAA,UACA,OAAO,iBAAiB;AAAA,QAC1B;AAAA,MACF;AAAA,IACF;AAGA,UAAM,WAAW,KAAK,cAAc,KAAK;AACzC,WAAO,SAAS,OAAO;AAGvB,UAAM,IAAI,MAAM,8BAA8B;AAAA,EAChD;AAAA,EAEQ,mBAAqF;AAC3F,UAAM,YAAY,IAAI,gBAAgB,OAAO,SAAS,MAAM;AAC5D,UAAM,OAAO,UAAU,IAAI,MAAM;AACjC,UAAM,QAAQ,UAAU,IAAI,OAAO;AACnC,UAAM,QAAQ,UAAU,IAAI,OAAO;AAEnC,QAAI,QAAQ,OAAO;AACjB,aAAO;AAAA,QACL,MAAM,QAAQ;AAAA,QACd,OAAO,SAAS;AAAA,QAChB,eAAe,SAAS;AAAA,MAC1B;AAAA,IACF;AAEA,WAAO;AAAA,EACT;AAAA,EAEQ,cAAc,OAAuB;AAC3C,UAAM,MAAM,IAAI,IAAI,KAAK,OAAO,QAAQ;AAExC,QAAI,aAAa,IAAI,gBAAgB,KAAK,OAAO,WAAW;AAC5D,QAAI,aAAa,IAAI,SAAS,KAAK;AAGnC,QAAI,KAAK,OAAO,sBAAsB;AACpC,aAAO,QAAQ,KAAK,OAAO,oBAAoB,EAAE,QAAQ,CAAC,CAAC,KAAK,KAAK,MAAM;AACzE,YAAI,aAAa,IAAI,KAAK,KAAK;AAAA,MACjC,CAAC;AAAA,IACH;AAEA,WAAO,IAAI,SAAS;AAAA,EACtB;AAAA,EAEQ,gBAAwB;AAE9B,QAAI,SAAS;AACb,UAAM,QAAQ;AACd,aAAS,IAAI,GAAG,IAAI,IAAI,KAAK;AAC3B,gBAAU,MAAM,OAAO,KAAK,MAAM,KAAK,OAAO,IAAI,MAAM,MAAM,CAAC;AAAA,IACjE;AACA,WAAO;AAAA,EACT;AACF;;;AC9EO,SAAS,kBAAkB,QAAkC;AAClE,SAAO,IAAI,eAAe,MAAM;AAClC;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@idealyst/oauth-client",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Universal OAuth2 client for web and React Native",
|
|
5
5
|
"main": "src/index.ts",
|
|
6
6
|
"module": "src/index.ts",
|
|
@@ -53,4 +53,4 @@
|
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
|
55
55
|
}
|
|
56
|
-
}
|
|
56
|
+
}
|