@adakrpos/auth 0.0.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/README.md +221 -0
- package/__tests__/basic.test.ts +7 -0
- package/__tests__/client.test.ts +206 -0
- package/__tests__/express.test.ts +241 -0
- package/__tests__/hono.test.ts +173 -0
- package/dist/client-Dd5DjxzG.d.mts +64 -0
- package/dist/client-Dd5DjxzG.d.ts +64 -0
- package/dist/express.d.mts +13 -0
- package/dist/express.d.ts +13 -0
- package/dist/express.js +173 -0
- package/dist/express.mjs +145 -0
- package/dist/generic.d.mts +5 -0
- package/dist/generic.d.ts +5 -0
- package/dist/generic.js +144 -0
- package/dist/generic.mjs +117 -0
- package/dist/hono.d.mts +17 -0
- package/dist/hono.d.ts +17 -0
- package/dist/hono.js +182 -0
- package/dist/hono.mjs +155 -0
- package/dist/index.d.mts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +118 -0
- package/dist/index.mjs +88 -0
- package/package.json +48 -0
- package/src/cache.ts +38 -0
- package/src/client.ts +93 -0
- package/src/express.ts +94 -0
- package/src/generic.ts +50 -0
- package/src/hono.ts +100 -0
- package/src/index.ts +8 -0
- package/src/types.ts +54 -0
- package/tsconfig.json +13 -0
- package/tsup.config.ts +15 -0
- package/vitest.config.ts +7 -0
package/dist/generic.js
ADDED
|
@@ -0,0 +1,144 @@
|
|
|
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/generic.ts
|
|
21
|
+
var generic_exports = {};
|
|
22
|
+
__export(generic_exports, {
|
|
23
|
+
verifyRequest: () => verifyRequest
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(generic_exports);
|
|
26
|
+
|
|
27
|
+
// src/cache.ts
|
|
28
|
+
var cache = /* @__PURE__ */ new Map();
|
|
29
|
+
var DEFAULT_CACHE_TTL_MS = 3e4;
|
|
30
|
+
function getCachedApiKeyValidity(apiKey) {
|
|
31
|
+
const entry = cache.get(apiKey);
|
|
32
|
+
if (!entry) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
if (Date.now() > entry.expiresAt) {
|
|
36
|
+
cache.delete(apiKey);
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return entry.valid;
|
|
40
|
+
}
|
|
41
|
+
function setCachedApiKeyValidity(apiKey, valid, ttlMs = DEFAULT_CACHE_TTL_MS) {
|
|
42
|
+
cache.set(apiKey, {
|
|
43
|
+
valid,
|
|
44
|
+
expiresAt: Date.now() + ttlMs
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// src/client.ts
|
|
49
|
+
var DEFAULT_AUTH_URL = "https://ada-kr-pos.com";
|
|
50
|
+
function createUrl(baseUrl, path) {
|
|
51
|
+
return new URL(path, baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`).toString();
|
|
52
|
+
}
|
|
53
|
+
async function parseJson(response) {
|
|
54
|
+
return await response.json();
|
|
55
|
+
}
|
|
56
|
+
function createAdakrposAuth(config) {
|
|
57
|
+
const baseUrl = config.authUrl ?? DEFAULT_AUTH_URL;
|
|
58
|
+
const apiKey = config.apiKey;
|
|
59
|
+
async function request(path, init, options = {}) {
|
|
60
|
+
if (getCachedApiKeyValidity(apiKey) === false) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const response = await fetch(createUrl(baseUrl, path), {
|
|
64
|
+
...init,
|
|
65
|
+
headers: {
|
|
66
|
+
Authorization: `Bearer ${apiKey}`,
|
|
67
|
+
...init.body ? { "Content-Type": "application/json" } : {},
|
|
68
|
+
...init.headers
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
if (response.status === 401 || response.status === 403) {
|
|
72
|
+
setCachedApiKeyValidity(apiKey, false);
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
setCachedApiKeyValidity(apiKey, true);
|
|
76
|
+
if (response.status === 404 && options.returnNullOnNotFound) {
|
|
77
|
+
return null;
|
|
78
|
+
}
|
|
79
|
+
if (!response.ok) {
|
|
80
|
+
throw new Error(`Adakrpos auth request failed with status ${response.status}`);
|
|
81
|
+
}
|
|
82
|
+
return parseJson(response);
|
|
83
|
+
}
|
|
84
|
+
return {
|
|
85
|
+
async verifySession(sessionId) {
|
|
86
|
+
return request(
|
|
87
|
+
"/api/sdk/verify-session",
|
|
88
|
+
{
|
|
89
|
+
method: "POST",
|
|
90
|
+
body: JSON.stringify({ sessionId })
|
|
91
|
+
},
|
|
92
|
+
{ returnNullOnNotFound: true }
|
|
93
|
+
);
|
|
94
|
+
},
|
|
95
|
+
async getUser(userId) {
|
|
96
|
+
return request(`/api/sdk/users/${encodeURIComponent(userId)}`, {
|
|
97
|
+
method: "GET"
|
|
98
|
+
}, { returnNullOnNotFound: true });
|
|
99
|
+
},
|
|
100
|
+
async getCurrentUser(sessionId) {
|
|
101
|
+
const result = await this.verifySession(sessionId);
|
|
102
|
+
return result?.user ?? null;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// src/generic.ts
|
|
108
|
+
var UNAUTH_CONTEXT = {
|
|
109
|
+
user: null,
|
|
110
|
+
session: null,
|
|
111
|
+
isAuthenticated: false
|
|
112
|
+
};
|
|
113
|
+
function getSessionId(cookieHeader) {
|
|
114
|
+
const sessionMatch = cookieHeader.match(/(?:^|;\s*)adakrpos_session=([^;]+)/);
|
|
115
|
+
if (!sessionMatch) {
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
try {
|
|
119
|
+
return decodeURIComponent(sessionMatch[1]);
|
|
120
|
+
} catch {
|
|
121
|
+
return sessionMatch[1];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
async function verifyRequest(request, config) {
|
|
125
|
+
const client = createAdakrposAuth(config);
|
|
126
|
+
const cookieHeader = request.headers.get("Cookie") ?? "";
|
|
127
|
+
const sessionId = getSessionId(cookieHeader);
|
|
128
|
+
if (!sessionId) {
|
|
129
|
+
return UNAUTH_CONTEXT;
|
|
130
|
+
}
|
|
131
|
+
const result = await client.verifySession(sessionId);
|
|
132
|
+
if (!result) {
|
|
133
|
+
return UNAUTH_CONTEXT;
|
|
134
|
+
}
|
|
135
|
+
return {
|
|
136
|
+
user: result.user,
|
|
137
|
+
session: result.session,
|
|
138
|
+
isAuthenticated: true
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
142
|
+
0 && (module.exports = {
|
|
143
|
+
verifyRequest
|
|
144
|
+
});
|
package/dist/generic.mjs
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// src/cache.ts
|
|
2
|
+
var cache = /* @__PURE__ */ new Map();
|
|
3
|
+
var DEFAULT_CACHE_TTL_MS = 3e4;
|
|
4
|
+
function getCachedApiKeyValidity(apiKey) {
|
|
5
|
+
const entry = cache.get(apiKey);
|
|
6
|
+
if (!entry) {
|
|
7
|
+
return null;
|
|
8
|
+
}
|
|
9
|
+
if (Date.now() > entry.expiresAt) {
|
|
10
|
+
cache.delete(apiKey);
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
return entry.valid;
|
|
14
|
+
}
|
|
15
|
+
function setCachedApiKeyValidity(apiKey, valid, ttlMs = DEFAULT_CACHE_TTL_MS) {
|
|
16
|
+
cache.set(apiKey, {
|
|
17
|
+
valid,
|
|
18
|
+
expiresAt: Date.now() + ttlMs
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/client.ts
|
|
23
|
+
var DEFAULT_AUTH_URL = "https://ada-kr-pos.com";
|
|
24
|
+
function createUrl(baseUrl, path) {
|
|
25
|
+
return new URL(path, baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`).toString();
|
|
26
|
+
}
|
|
27
|
+
async function parseJson(response) {
|
|
28
|
+
return await response.json();
|
|
29
|
+
}
|
|
30
|
+
function createAdakrposAuth(config) {
|
|
31
|
+
const baseUrl = config.authUrl ?? DEFAULT_AUTH_URL;
|
|
32
|
+
const apiKey = config.apiKey;
|
|
33
|
+
async function request(path, init, options = {}) {
|
|
34
|
+
if (getCachedApiKeyValidity(apiKey) === false) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
const response = await fetch(createUrl(baseUrl, path), {
|
|
38
|
+
...init,
|
|
39
|
+
headers: {
|
|
40
|
+
Authorization: `Bearer ${apiKey}`,
|
|
41
|
+
...init.body ? { "Content-Type": "application/json" } : {},
|
|
42
|
+
...init.headers
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
if (response.status === 401 || response.status === 403) {
|
|
46
|
+
setCachedApiKeyValidity(apiKey, false);
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
setCachedApiKeyValidity(apiKey, true);
|
|
50
|
+
if (response.status === 404 && options.returnNullOnNotFound) {
|
|
51
|
+
return null;
|
|
52
|
+
}
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
throw new Error(`Adakrpos auth request failed with status ${response.status}`);
|
|
55
|
+
}
|
|
56
|
+
return parseJson(response);
|
|
57
|
+
}
|
|
58
|
+
return {
|
|
59
|
+
async verifySession(sessionId) {
|
|
60
|
+
return request(
|
|
61
|
+
"/api/sdk/verify-session",
|
|
62
|
+
{
|
|
63
|
+
method: "POST",
|
|
64
|
+
body: JSON.stringify({ sessionId })
|
|
65
|
+
},
|
|
66
|
+
{ returnNullOnNotFound: true }
|
|
67
|
+
);
|
|
68
|
+
},
|
|
69
|
+
async getUser(userId) {
|
|
70
|
+
return request(`/api/sdk/users/${encodeURIComponent(userId)}`, {
|
|
71
|
+
method: "GET"
|
|
72
|
+
}, { returnNullOnNotFound: true });
|
|
73
|
+
},
|
|
74
|
+
async getCurrentUser(sessionId) {
|
|
75
|
+
const result = await this.verifySession(sessionId);
|
|
76
|
+
return result?.user ?? null;
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// src/generic.ts
|
|
82
|
+
var UNAUTH_CONTEXT = {
|
|
83
|
+
user: null,
|
|
84
|
+
session: null,
|
|
85
|
+
isAuthenticated: false
|
|
86
|
+
};
|
|
87
|
+
function getSessionId(cookieHeader) {
|
|
88
|
+
const sessionMatch = cookieHeader.match(/(?:^|;\s*)adakrpos_session=([^;]+)/);
|
|
89
|
+
if (!sessionMatch) {
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
try {
|
|
93
|
+
return decodeURIComponent(sessionMatch[1]);
|
|
94
|
+
} catch {
|
|
95
|
+
return sessionMatch[1];
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function verifyRequest(request, config) {
|
|
99
|
+
const client = createAdakrposAuth(config);
|
|
100
|
+
const cookieHeader = request.headers.get("Cookie") ?? "";
|
|
101
|
+
const sessionId = getSessionId(cookieHeader);
|
|
102
|
+
if (!sessionId) {
|
|
103
|
+
return UNAUTH_CONTEXT;
|
|
104
|
+
}
|
|
105
|
+
const result = await client.verifySession(sessionId);
|
|
106
|
+
if (!result) {
|
|
107
|
+
return UNAUTH_CONTEXT;
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
user: result.user,
|
|
111
|
+
session: result.session,
|
|
112
|
+
isAuthenticated: true
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
export {
|
|
116
|
+
verifyRequest
|
|
117
|
+
};
|
package/dist/hono.d.mts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as hono from 'hono';
|
|
2
|
+
import { Context } from 'hono';
|
|
3
|
+
import { A as AuthContext, a as AdakrposAuthConfig } from './client-Dd5DjxzG.mjs';
|
|
4
|
+
|
|
5
|
+
type AuthFn = () => Promise<AuthContext>;
|
|
6
|
+
declare module "hono" {
|
|
7
|
+
interface ContextVariableMap {
|
|
8
|
+
auth: AuthFn;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
declare function adakrposAuth(config: AdakrposAuthConfig): hono.MiddlewareHandler<any, string, {}, Response>;
|
|
12
|
+
declare function getAuth(c: Context): Promise<AuthContext>;
|
|
13
|
+
declare function requireAuth(config: AdakrposAuthConfig): hono.MiddlewareHandler<any, string, {}, Response | (Response & hono.TypedResponse<{
|
|
14
|
+
error: string;
|
|
15
|
+
}, 401, "json">)>;
|
|
16
|
+
|
|
17
|
+
export { adakrposAuth, getAuth, requireAuth };
|
package/dist/hono.d.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import * as hono from 'hono';
|
|
2
|
+
import { Context } from 'hono';
|
|
3
|
+
import { A as AuthContext, a as AdakrposAuthConfig } from './client-Dd5DjxzG.js';
|
|
4
|
+
|
|
5
|
+
type AuthFn = () => Promise<AuthContext>;
|
|
6
|
+
declare module "hono" {
|
|
7
|
+
interface ContextVariableMap {
|
|
8
|
+
auth: AuthFn;
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
declare function adakrposAuth(config: AdakrposAuthConfig): hono.MiddlewareHandler<any, string, {}, Response>;
|
|
12
|
+
declare function getAuth(c: Context): Promise<AuthContext>;
|
|
13
|
+
declare function requireAuth(config: AdakrposAuthConfig): hono.MiddlewareHandler<any, string, {}, Response | (Response & hono.TypedResponse<{
|
|
14
|
+
error: string;
|
|
15
|
+
}, 401, "json">)>;
|
|
16
|
+
|
|
17
|
+
export { adakrposAuth, getAuth, requireAuth };
|
package/dist/hono.js
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
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/hono.ts
|
|
21
|
+
var hono_exports = {};
|
|
22
|
+
__export(hono_exports, {
|
|
23
|
+
adakrposAuth: () => adakrposAuth,
|
|
24
|
+
getAuth: () => getAuth,
|
|
25
|
+
requireAuth: () => requireAuth
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(hono_exports);
|
|
28
|
+
var import_factory = require("hono/factory");
|
|
29
|
+
|
|
30
|
+
// src/cache.ts
|
|
31
|
+
var cache = /* @__PURE__ */ new Map();
|
|
32
|
+
var DEFAULT_CACHE_TTL_MS = 3e4;
|
|
33
|
+
function getCachedApiKeyValidity(apiKey) {
|
|
34
|
+
const entry = cache.get(apiKey);
|
|
35
|
+
if (!entry) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
if (Date.now() > entry.expiresAt) {
|
|
39
|
+
cache.delete(apiKey);
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
return entry.valid;
|
|
43
|
+
}
|
|
44
|
+
function setCachedApiKeyValidity(apiKey, valid, ttlMs = DEFAULT_CACHE_TTL_MS) {
|
|
45
|
+
cache.set(apiKey, {
|
|
46
|
+
valid,
|
|
47
|
+
expiresAt: Date.now() + ttlMs
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// src/client.ts
|
|
52
|
+
var DEFAULT_AUTH_URL = "https://ada-kr-pos.com";
|
|
53
|
+
function createUrl(baseUrl, path) {
|
|
54
|
+
return new URL(path, baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`).toString();
|
|
55
|
+
}
|
|
56
|
+
async function parseJson(response) {
|
|
57
|
+
return await response.json();
|
|
58
|
+
}
|
|
59
|
+
function createAdakrposAuth(config) {
|
|
60
|
+
const baseUrl = config.authUrl ?? DEFAULT_AUTH_URL;
|
|
61
|
+
const apiKey = config.apiKey;
|
|
62
|
+
async function request(path, init, options = {}) {
|
|
63
|
+
if (getCachedApiKeyValidity(apiKey) === false) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const response = await fetch(createUrl(baseUrl, path), {
|
|
67
|
+
...init,
|
|
68
|
+
headers: {
|
|
69
|
+
Authorization: `Bearer ${apiKey}`,
|
|
70
|
+
...init.body ? { "Content-Type": "application/json" } : {},
|
|
71
|
+
...init.headers
|
|
72
|
+
}
|
|
73
|
+
});
|
|
74
|
+
if (response.status === 401 || response.status === 403) {
|
|
75
|
+
setCachedApiKeyValidity(apiKey, false);
|
|
76
|
+
return null;
|
|
77
|
+
}
|
|
78
|
+
setCachedApiKeyValidity(apiKey, true);
|
|
79
|
+
if (response.status === 404 && options.returnNullOnNotFound) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
if (!response.ok) {
|
|
83
|
+
throw new Error(`Adakrpos auth request failed with status ${response.status}`);
|
|
84
|
+
}
|
|
85
|
+
return parseJson(response);
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
async verifySession(sessionId) {
|
|
89
|
+
return request(
|
|
90
|
+
"/api/sdk/verify-session",
|
|
91
|
+
{
|
|
92
|
+
method: "POST",
|
|
93
|
+
body: JSON.stringify({ sessionId })
|
|
94
|
+
},
|
|
95
|
+
{ returnNullOnNotFound: true }
|
|
96
|
+
);
|
|
97
|
+
},
|
|
98
|
+
async getUser(userId) {
|
|
99
|
+
return request(`/api/sdk/users/${encodeURIComponent(userId)}`, {
|
|
100
|
+
method: "GET"
|
|
101
|
+
}, { returnNullOnNotFound: true });
|
|
102
|
+
},
|
|
103
|
+
async getCurrentUser(sessionId) {
|
|
104
|
+
const result = await this.verifySession(sessionId);
|
|
105
|
+
return result?.user ?? null;
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// src/hono.ts
|
|
111
|
+
var UNAUTH_CONTEXT = {
|
|
112
|
+
user: null,
|
|
113
|
+
session: null,
|
|
114
|
+
isAuthenticated: false
|
|
115
|
+
};
|
|
116
|
+
function getSessionId(cookieHeader) {
|
|
117
|
+
const sessionMatch = cookieHeader.match(/(?:^|;\s*)adakrpos_session=([^;]+)/);
|
|
118
|
+
if (!sessionMatch) {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
try {
|
|
122
|
+
return decodeURIComponent(sessionMatch[1]);
|
|
123
|
+
} catch {
|
|
124
|
+
return sessionMatch[1];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function createAuthFn(client, sessionId) {
|
|
128
|
+
let authPromise;
|
|
129
|
+
return async () => {
|
|
130
|
+
if (!authPromise) {
|
|
131
|
+
authPromise = (async () => {
|
|
132
|
+
if (!sessionId) {
|
|
133
|
+
return UNAUTH_CONTEXT;
|
|
134
|
+
}
|
|
135
|
+
const result = await client.verifySession(sessionId);
|
|
136
|
+
if (!result) {
|
|
137
|
+
return UNAUTH_CONTEXT;
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
user: result.user,
|
|
141
|
+
session: result.session,
|
|
142
|
+
isAuthenticated: true
|
|
143
|
+
};
|
|
144
|
+
})();
|
|
145
|
+
}
|
|
146
|
+
return authPromise;
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
function setAuthContext(c, client) {
|
|
150
|
+
const cookieHeader = c.req.header("Cookie") ?? "";
|
|
151
|
+
c.set("auth", createAuthFn(client, getSessionId(cookieHeader)));
|
|
152
|
+
}
|
|
153
|
+
function adakrposAuth(config) {
|
|
154
|
+
const client = createAdakrposAuth(config);
|
|
155
|
+
return (0, import_factory.createMiddleware)(async (c, next) => {
|
|
156
|
+
setAuthContext(c, client);
|
|
157
|
+
await next();
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
async function getAuth(c) {
|
|
161
|
+
const authFn = c.get("auth");
|
|
162
|
+
if (!authFn) {
|
|
163
|
+
return UNAUTH_CONTEXT;
|
|
164
|
+
}
|
|
165
|
+
return authFn();
|
|
166
|
+
}
|
|
167
|
+
function requireAuth(config) {
|
|
168
|
+
const client = createAdakrposAuth(config);
|
|
169
|
+
return (0, import_factory.createMiddleware)(async (c, next) => {
|
|
170
|
+
setAuthContext(c, client);
|
|
171
|
+
if (!(await getAuth(c)).isAuthenticated) {
|
|
172
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
173
|
+
}
|
|
174
|
+
await next();
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
178
|
+
0 && (module.exports = {
|
|
179
|
+
adakrposAuth,
|
|
180
|
+
getAuth,
|
|
181
|
+
requireAuth
|
|
182
|
+
});
|
package/dist/hono.mjs
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
// src/hono.ts
|
|
2
|
+
import { createMiddleware } from "hono/factory";
|
|
3
|
+
|
|
4
|
+
// src/cache.ts
|
|
5
|
+
var cache = /* @__PURE__ */ new Map();
|
|
6
|
+
var DEFAULT_CACHE_TTL_MS = 3e4;
|
|
7
|
+
function getCachedApiKeyValidity(apiKey) {
|
|
8
|
+
const entry = cache.get(apiKey);
|
|
9
|
+
if (!entry) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
if (Date.now() > entry.expiresAt) {
|
|
13
|
+
cache.delete(apiKey);
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
return entry.valid;
|
|
17
|
+
}
|
|
18
|
+
function setCachedApiKeyValidity(apiKey, valid, ttlMs = DEFAULT_CACHE_TTL_MS) {
|
|
19
|
+
cache.set(apiKey, {
|
|
20
|
+
valid,
|
|
21
|
+
expiresAt: Date.now() + ttlMs
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// src/client.ts
|
|
26
|
+
var DEFAULT_AUTH_URL = "https://ada-kr-pos.com";
|
|
27
|
+
function createUrl(baseUrl, path) {
|
|
28
|
+
return new URL(path, baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`).toString();
|
|
29
|
+
}
|
|
30
|
+
async function parseJson(response) {
|
|
31
|
+
return await response.json();
|
|
32
|
+
}
|
|
33
|
+
function createAdakrposAuth(config) {
|
|
34
|
+
const baseUrl = config.authUrl ?? DEFAULT_AUTH_URL;
|
|
35
|
+
const apiKey = config.apiKey;
|
|
36
|
+
async function request(path, init, options = {}) {
|
|
37
|
+
if (getCachedApiKeyValidity(apiKey) === false) {
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
const response = await fetch(createUrl(baseUrl, path), {
|
|
41
|
+
...init,
|
|
42
|
+
headers: {
|
|
43
|
+
Authorization: `Bearer ${apiKey}`,
|
|
44
|
+
...init.body ? { "Content-Type": "application/json" } : {},
|
|
45
|
+
...init.headers
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
if (response.status === 401 || response.status === 403) {
|
|
49
|
+
setCachedApiKeyValidity(apiKey, false);
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
setCachedApiKeyValidity(apiKey, true);
|
|
53
|
+
if (response.status === 404 && options.returnNullOnNotFound) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
if (!response.ok) {
|
|
57
|
+
throw new Error(`Adakrpos auth request failed with status ${response.status}`);
|
|
58
|
+
}
|
|
59
|
+
return parseJson(response);
|
|
60
|
+
}
|
|
61
|
+
return {
|
|
62
|
+
async verifySession(sessionId) {
|
|
63
|
+
return request(
|
|
64
|
+
"/api/sdk/verify-session",
|
|
65
|
+
{
|
|
66
|
+
method: "POST",
|
|
67
|
+
body: JSON.stringify({ sessionId })
|
|
68
|
+
},
|
|
69
|
+
{ returnNullOnNotFound: true }
|
|
70
|
+
);
|
|
71
|
+
},
|
|
72
|
+
async getUser(userId) {
|
|
73
|
+
return request(`/api/sdk/users/${encodeURIComponent(userId)}`, {
|
|
74
|
+
method: "GET"
|
|
75
|
+
}, { returnNullOnNotFound: true });
|
|
76
|
+
},
|
|
77
|
+
async getCurrentUser(sessionId) {
|
|
78
|
+
const result = await this.verifySession(sessionId);
|
|
79
|
+
return result?.user ?? null;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/hono.ts
|
|
85
|
+
var UNAUTH_CONTEXT = {
|
|
86
|
+
user: null,
|
|
87
|
+
session: null,
|
|
88
|
+
isAuthenticated: false
|
|
89
|
+
};
|
|
90
|
+
function getSessionId(cookieHeader) {
|
|
91
|
+
const sessionMatch = cookieHeader.match(/(?:^|;\s*)adakrpos_session=([^;]+)/);
|
|
92
|
+
if (!sessionMatch) {
|
|
93
|
+
return null;
|
|
94
|
+
}
|
|
95
|
+
try {
|
|
96
|
+
return decodeURIComponent(sessionMatch[1]);
|
|
97
|
+
} catch {
|
|
98
|
+
return sessionMatch[1];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
function createAuthFn(client, sessionId) {
|
|
102
|
+
let authPromise;
|
|
103
|
+
return async () => {
|
|
104
|
+
if (!authPromise) {
|
|
105
|
+
authPromise = (async () => {
|
|
106
|
+
if (!sessionId) {
|
|
107
|
+
return UNAUTH_CONTEXT;
|
|
108
|
+
}
|
|
109
|
+
const result = await client.verifySession(sessionId);
|
|
110
|
+
if (!result) {
|
|
111
|
+
return UNAUTH_CONTEXT;
|
|
112
|
+
}
|
|
113
|
+
return {
|
|
114
|
+
user: result.user,
|
|
115
|
+
session: result.session,
|
|
116
|
+
isAuthenticated: true
|
|
117
|
+
};
|
|
118
|
+
})();
|
|
119
|
+
}
|
|
120
|
+
return authPromise;
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
function setAuthContext(c, client) {
|
|
124
|
+
const cookieHeader = c.req.header("Cookie") ?? "";
|
|
125
|
+
c.set("auth", createAuthFn(client, getSessionId(cookieHeader)));
|
|
126
|
+
}
|
|
127
|
+
function adakrposAuth(config) {
|
|
128
|
+
const client = createAdakrposAuth(config);
|
|
129
|
+
return createMiddleware(async (c, next) => {
|
|
130
|
+
setAuthContext(c, client);
|
|
131
|
+
await next();
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
async function getAuth(c) {
|
|
135
|
+
const authFn = c.get("auth");
|
|
136
|
+
if (!authFn) {
|
|
137
|
+
return UNAUTH_CONTEXT;
|
|
138
|
+
}
|
|
139
|
+
return authFn();
|
|
140
|
+
}
|
|
141
|
+
function requireAuth(config) {
|
|
142
|
+
const client = createAdakrposAuth(config);
|
|
143
|
+
return createMiddleware(async (c, next) => {
|
|
144
|
+
setAuthContext(c, client);
|
|
145
|
+
if (!(await getAuth(c)).isAuthenticated) {
|
|
146
|
+
return c.json({ error: "Unauthorized" }, 401);
|
|
147
|
+
}
|
|
148
|
+
await next();
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
export {
|
|
152
|
+
adakrposAuth,
|
|
153
|
+
getAuth,
|
|
154
|
+
requireAuth
|
|
155
|
+
};
|
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { b as AdakrposAuthClient, a as AdakrposAuthConfig, c as AdakrposAuthContext, d as AdakrposSession, e as AdakrposUnauthContext, f as AdakrposUser, g as ApiKeyInfo, A as AuthContext, D as DeveloperApp, h as createAdakrposAuth } from './client-Dd5DjxzG.mjs';
|
|
2
|
+
|
|
3
|
+
declare function getCachedApiKeyValidity(apiKey: string): boolean | null;
|
|
4
|
+
declare function setCachedApiKeyValidity(apiKey: string, valid: boolean, ttlMs?: number): void;
|
|
5
|
+
declare function clearApiKeyCache(): void;
|
|
6
|
+
|
|
7
|
+
export { clearApiKeyCache, getCachedApiKeyValidity, setCachedApiKeyValidity };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { b as AdakrposAuthClient, a as AdakrposAuthConfig, c as AdakrposAuthContext, d as AdakrposSession, e as AdakrposUnauthContext, f as AdakrposUser, g as ApiKeyInfo, A as AuthContext, D as DeveloperApp, h as createAdakrposAuth } from './client-Dd5DjxzG.js';
|
|
2
|
+
|
|
3
|
+
declare function getCachedApiKeyValidity(apiKey: string): boolean | null;
|
|
4
|
+
declare function setCachedApiKeyValidity(apiKey: string, valid: boolean, ttlMs?: number): void;
|
|
5
|
+
declare function clearApiKeyCache(): void;
|
|
6
|
+
|
|
7
|
+
export { clearApiKeyCache, getCachedApiKeyValidity, setCachedApiKeyValidity };
|