theauthapi 1.0.4 → 1.0.7
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 +284 -2
- package/dist/endpoints/Accounts/Accounts.d.ts +10 -0
- package/dist/endpoints/Accounts/Accounts.js +26 -0
- package/dist/endpoints/Accounts/AccountsInterface.d.ts +4 -0
- package/dist/endpoints/Accounts/AccountsInterface.js +2 -0
- package/dist/endpoints/ApiKeys/ApiKeys.d.ts +16 -0
- package/dist/endpoints/ApiKeys/ApiKeys.js +91 -0
- package/dist/endpoints/ApiKeys/ApiKeysInterface.d.ts +9 -0
- package/dist/endpoints/ApiKeys/ApiKeysInterface.js +2 -0
- package/dist/endpoints/Projects/Projects.d.ts +16 -0
- package/dist/endpoints/Projects/Projects.js +68 -0
- package/dist/endpoints/Projects/ProjectsInterface.d.ts +8 -0
- package/dist/endpoints/Projects/ProjectsInterface.js +2 -0
- package/dist/index.d.ts +36 -0
- package/dist/index.js +89 -0
- package/dist/libraryMeta.d.ts +1 -0
- package/dist/libraryMeta.js +4 -0
- package/dist/services/ApiRequest/ApiCall.d.ts +5 -0
- package/dist/services/ApiRequest/ApiCall.js +2 -0
- package/dist/services/ApiRequest/ApiRequest.d.ts +26 -0
- package/dist/services/ApiRequest/ApiRequest.js +117 -0
- package/dist/services/ApiRequest/ApiRequestError.d.ts +10 -0
- package/dist/services/ApiRequest/ApiRequestError.js +15 -0
- package/dist/services/ApiRequest/ApiResponseError.d.ts +12 -0
- package/dist/services/ApiRequest/ApiResponseError.js +17 -0
- package/dist/services/ApiRequest/HttpMethod.d.ts +7 -0
- package/dist/services/ApiRequest/HttpMethod.js +11 -0
- package/dist/types/index.d.ts +65 -0
- package/dist/types/index.js +13 -0
- package/dist/util/index.d.ts +1 -0
- package/dist/util/index.js +13 -0
- package/package.json +4 -9
- package/.eslintrc.js +0 -15
- package/.prettierrc.json +0 -1
- package/jest.config.js +0 -5
- package/jest.setup.ts +0 -0
- package/src/endpoints/ApiKeys/ApiKeys.ts +0 -68
- package/src/endpoints/ApiKeys/ApiKeysInterface.ts +0 -11
- package/src/endpoints/Projects/Projects.ts +0 -62
- package/src/endpoints/Projects/ProjectsInterface.ts +0 -9
- package/src/index.ts +0 -103
- package/src/libraryMeta.ts +0 -1
- package/src/services/ApiRequest/ApiCall.ts +0 -7
- package/src/services/ApiRequest/ApiRequest.ts +0 -132
- package/src/services/ApiRequest/ApiRequestError.ts +0 -7
- package/src/services/ApiRequest/HttpMethod.ts +0 -7
- package/src/tests/endpoints/apiKeys.spec.ts +0 -102
- package/src/tests/endpoints/projects.spec.ts +0 -105
- package/src/tests/index.spec.ts +0 -35
- package/src/tests/testServer/middleware/middleware.ts +0 -17
- package/src/tests/testServer/routes/apiKeys.ts +0 -89
- package/src/tests/testServer/routes/auth.ts +0 -24
- package/src/tests/testServer/routes/projects.ts +0 -87
- package/src/tests/testServer/server.ts +0 -16
- package/src/types/index.ts +0 -59
- package/src/util/index.ts +0 -7
- package/tsconfig.json +0 -102
package/src/index.ts
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import assert from "assert";
|
|
2
|
-
import removeSlash from "remove-trailing-slash";
|
|
3
|
-
|
|
4
|
-
import ApiRequest from "./services/ApiRequest/ApiRequest";
|
|
5
|
-
import { validateString } from "./util";
|
|
6
|
-
import ApiKeys from "./endpoints/ApiKeys/ApiKeys";
|
|
7
|
-
import { HttpMethod } from "./services/ApiRequest/HttpMethod";
|
|
8
|
-
import Projects from "./endpoints/Projects/Projects";
|
|
9
|
-
|
|
10
|
-
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
|
11
|
-
const noop = () => {};
|
|
12
|
-
|
|
13
|
-
type Options = {
|
|
14
|
-
host?: string;
|
|
15
|
-
timeout?: string | number;
|
|
16
|
-
cacheTTL?: number;
|
|
17
|
-
enable?: boolean;
|
|
18
|
-
retryCount?: number;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
class TheAuthAPI {
|
|
22
|
-
queue: [];
|
|
23
|
-
accessKey: string;
|
|
24
|
-
host: string;
|
|
25
|
-
timeout: number | string | undefined;
|
|
26
|
-
cacheTTL: number;
|
|
27
|
-
api: ApiRequest;
|
|
28
|
-
apiKeys: ApiKeys;
|
|
29
|
-
projects: Projects;
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* Initialize a new `Analytics` with your Segment project's `writeKey` and an
|
|
33
|
-
* optional dictionary of `options`.
|
|
34
|
-
*
|
|
35
|
-
* @param {String} accessKey
|
|
36
|
-
* @param {Object} [options] (optional)
|
|
37
|
-
* @property {Number} flushAt (default: 20)
|
|
38
|
-
* @property {Number} flushInterval (default: 10000)
|
|
39
|
-
* @property {String} host (default: 'https://api.segment.io')
|
|
40
|
-
* @property {Boolean} enable (default: true)
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
constructor(accessKey: string, options?: Options) {
|
|
44
|
-
assert(accessKey, "You must pass your project's write key.");
|
|
45
|
-
this.queue = [];
|
|
46
|
-
this.accessKey = accessKey;
|
|
47
|
-
this.host = removeSlash(options?.host || "https://api.theauthapi.com");
|
|
48
|
-
this.timeout = options?.timeout;
|
|
49
|
-
this.cacheTTL = options?.cacheTTL ?? 60;
|
|
50
|
-
this.api = new ApiRequest({
|
|
51
|
-
accessKey: this.accessKey,
|
|
52
|
-
host: this.host,
|
|
53
|
-
});
|
|
54
|
-
this.apiKeys = new ApiKeys(this.api);
|
|
55
|
-
this.projects = new Projects(this.api);
|
|
56
|
-
|
|
57
|
-
Object.defineProperty(this, "enable", {
|
|
58
|
-
configurable: false,
|
|
59
|
-
writable: false,
|
|
60
|
-
enumerable: true,
|
|
61
|
-
value: typeof options?.enable === "boolean" ? options.enable : true,
|
|
62
|
-
});
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/*
|
|
66
|
-
@deprecated
|
|
67
|
-
*/
|
|
68
|
-
async authenticateAPIKey(
|
|
69
|
-
key: string,
|
|
70
|
-
callback?: (err: any, data: any) => any
|
|
71
|
-
) {
|
|
72
|
-
validateString("key", key);
|
|
73
|
-
|
|
74
|
-
const cb = callback || noop;
|
|
75
|
-
const done = (err: any) => {
|
|
76
|
-
cb(err, data);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
const data = {
|
|
80
|
-
credentials: { api_key: key },
|
|
81
|
-
timestamp: new Date().getTime(),
|
|
82
|
-
sentAt: new Date().getTime(),
|
|
83
|
-
};
|
|
84
|
-
|
|
85
|
-
try {
|
|
86
|
-
const key = await this.api.request(
|
|
87
|
-
HttpMethod.POST,
|
|
88
|
-
"/auth/authenticate",
|
|
89
|
-
data
|
|
90
|
-
);
|
|
91
|
-
done(key);
|
|
92
|
-
return key;
|
|
93
|
-
} catch (err: any) {
|
|
94
|
-
if (err.response) {
|
|
95
|
-
const error = new Error(err.response.statusText);
|
|
96
|
-
return done(error);
|
|
97
|
-
}
|
|
98
|
-
done(err);
|
|
99
|
-
}
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export default TheAuthAPI;
|
package/src/libraryMeta.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const version = "1.0.1";
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import axios from "axios";
|
|
2
|
-
import { version } from "../../libraryMeta";
|
|
3
|
-
import { HttpMethod } from "./HttpMethod";
|
|
4
|
-
import ApiCall from "./ApiCall";
|
|
5
|
-
import ApiRequestError from "./ApiRequestError";
|
|
6
|
-
import axiosRetry from "axios-retry";
|
|
7
|
-
import ms from "ms";
|
|
8
|
-
|
|
9
|
-
type Config = {
|
|
10
|
-
host: string;
|
|
11
|
-
accessKey: string;
|
|
12
|
-
headers?: object;
|
|
13
|
-
retryCount?: number;
|
|
14
|
-
timeout?: number | string;
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
class ApiRequest implements ApiCall {
|
|
18
|
-
host: string;
|
|
19
|
-
headers: object;
|
|
20
|
-
accessKey: string;
|
|
21
|
-
timeout: number;
|
|
22
|
-
retryCount: number;
|
|
23
|
-
|
|
24
|
-
constructor(config: Config) {
|
|
25
|
-
const { host, accessKey, headers, retryCount, timeout } = config;
|
|
26
|
-
this.host = host;
|
|
27
|
-
this.accessKey = accessKey;
|
|
28
|
-
this.headers = this._generateDefaultHeaders();
|
|
29
|
-
this.timeout = timeout ? (typeof timeout === "string" ? ms(timeout) : timeout) : 0;
|
|
30
|
-
this.retryCount = retryCount ?? 3;
|
|
31
|
-
|
|
32
|
-
if (headers) {
|
|
33
|
-
this.headers = { ...this.headers, headers };
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
this._init();
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
_init() {
|
|
40
|
-
const isoDateFormat = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d*)?(?:[-+]\d{2}:?\d{2}|Z)?$/
|
|
41
|
-
function isIsoDateString(value: any): boolean {
|
|
42
|
-
return value && typeof value === "string" && isoDateFormat.test(value);
|
|
43
|
-
}
|
|
44
|
-
function handleDates(body: any) {
|
|
45
|
-
if (body === null || body === undefined || typeof body !== "object"){
|
|
46
|
-
return body;
|
|
47
|
-
}
|
|
48
|
-
for (const key of Object.keys(body)) {
|
|
49
|
-
const value = body[key];
|
|
50
|
-
if (isIsoDateString(value)) {
|
|
51
|
-
body[key] = new Date(value);
|
|
52
|
-
} else if (typeof value === "object") {
|
|
53
|
-
handleDates(value);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
axios.interceptors.response.use((response) => {
|
|
59
|
-
handleDates(response.data);
|
|
60
|
-
return response;
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
axiosRetry(axios, {
|
|
64
|
-
retries: this.retryCount ?? 3,
|
|
65
|
-
retryCondition: this._isErrorRetryable,
|
|
66
|
-
retryDelay: axiosRetry.exponentialDelay,
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
async request<T>(
|
|
71
|
-
method: HttpMethod,
|
|
72
|
-
endpoint: string,
|
|
73
|
-
payload?: any
|
|
74
|
-
): Promise<T> {
|
|
75
|
-
try {
|
|
76
|
-
const response = await axios.request<T>({
|
|
77
|
-
baseURL: this.host,
|
|
78
|
-
method: method,
|
|
79
|
-
url: endpoint,
|
|
80
|
-
data: payload,
|
|
81
|
-
headers: this.headers,
|
|
82
|
-
});
|
|
83
|
-
return response.data;
|
|
84
|
-
} catch (error) {
|
|
85
|
-
if (axios.isAxiosError(error)) {
|
|
86
|
-
if (error.response) {
|
|
87
|
-
throw new ApiRequestError(
|
|
88
|
-
error.response.status,
|
|
89
|
-
error.response.data.message ?? error.response.statusText
|
|
90
|
-
);
|
|
91
|
-
} else if (error.request) {
|
|
92
|
-
throw new Error(error.request);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
throw error;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
_generateDefaultHeaders() {
|
|
100
|
-
return {
|
|
101
|
-
"user-agent": `theauthapi-client-node/${version}`,
|
|
102
|
-
"x-api-key": this.accessKey,
|
|
103
|
-
"api-key": this.accessKey,
|
|
104
|
-
};
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
_isErrorRetryable(error: any) {
|
|
108
|
-
// Retry Network Errors.
|
|
109
|
-
if (axiosRetry.isNetworkError(error)) {
|
|
110
|
-
return true;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (!error.response) {
|
|
114
|
-
// Cannot determine if the request can be retried
|
|
115
|
-
return false;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// Retry Server Errors (5xx).
|
|
119
|
-
if (error.response.status >= 500 && error.response.status <= 599) {
|
|
120
|
-
return true;
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
// Retry if rate limited.
|
|
124
|
-
if (error.response.status === 429) {
|
|
125
|
-
return true;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return false;
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export default ApiRequest;
|
|
@@ -1,102 +0,0 @@
|
|
|
1
|
-
import TheAuthAPI from "../../index";
|
|
2
|
-
import testServer from "../testServer/server";
|
|
3
|
-
import { Server } from "http";
|
|
4
|
-
|
|
5
|
-
const port = 4063;
|
|
6
|
-
|
|
7
|
-
const createClient = (options?: any) => {
|
|
8
|
-
options = Object.assign(
|
|
9
|
-
{
|
|
10
|
-
host: `http://localhost:${port}`,
|
|
11
|
-
},
|
|
12
|
-
options
|
|
13
|
-
);
|
|
14
|
-
return new TheAuthAPI("access_key", options);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
describe("ApiKeys", () => {
|
|
18
|
-
let server: Server;
|
|
19
|
-
beforeAll(() => {
|
|
20
|
-
server = testServer.listen(port);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
afterAll(() => {
|
|
24
|
-
server.close();
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("should authenticate a valid api-key", async () => {
|
|
28
|
-
const client = createClient();
|
|
29
|
-
const data = await client.apiKeys.authenticateKey(
|
|
30
|
-
"live_access_zBA6cvuEbJEUhhDIWwuErXHLnwvWqtcqe2ajfV3RVVZvD6lc6xDUaSsSZL1fk53a"
|
|
31
|
-
);
|
|
32
|
-
console.log('day', data.createdAt.getDate(), 'month', data.createdAt.getMonth())
|
|
33
|
-
expect(data.name).toEqual("My customers first Api Key");
|
|
34
|
-
expect(data.key).toEqual(
|
|
35
|
-
"KGTSsxbDndjRRcpJGuQQp2or9UmQkqRrVQpCWgQruIXnvnNatmfdmOTcsgYnNwnH"
|
|
36
|
-
);
|
|
37
|
-
expect(data.createdAt).toEqual(new Date("2022-03-16T10:34:23.353Z"));
|
|
38
|
-
expect(data.updatedAt).toEqual(new Date("2022-03-16T10:34:23.353Z"));
|
|
39
|
-
expect(data.env).toEqual("live");
|
|
40
|
-
expect(data.customAccountId).toEqual("acc-id");
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("should list api keys", async () => {
|
|
44
|
-
const client = createClient();
|
|
45
|
-
const keys = await client.apiKeys.getKeys("project_1");
|
|
46
|
-
expect(keys).toEqual(
|
|
47
|
-
expect.arrayContaining([
|
|
48
|
-
expect.objectContaining({
|
|
49
|
-
key: "live_h3uDZInxQexGLkwoxMDmuqz6PsyXGjkbrmSTpEwFb8l97mdAlQKtt14kt9Rv91PL",
|
|
50
|
-
name: "my-first-api-key",
|
|
51
|
-
customMetaData: {},
|
|
52
|
-
customAccountId: null,
|
|
53
|
-
env: "live",
|
|
54
|
-
}),
|
|
55
|
-
expect.objectContaining({
|
|
56
|
-
key: "live_1OvRrfHbPdiCUrFAD4VwxiqEgg8L5uiVDlIgE4075juY7TnimZQG1Ll770irHyfM",
|
|
57
|
-
name: "my-second-api-key",
|
|
58
|
-
customMetaData: {},
|
|
59
|
-
customAccountId: null,
|
|
60
|
-
env: "live",
|
|
61
|
-
}),
|
|
62
|
-
])
|
|
63
|
-
);
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
it("should create a key", async () => {
|
|
67
|
-
const client = createClient();
|
|
68
|
-
const key = await client.apiKeys.createKey({
|
|
69
|
-
name: "my-new-api-key1",
|
|
70
|
-
projectId: "b52262b5-eaa6-4edd-825c-ebcdf76a10e5",
|
|
71
|
-
});
|
|
72
|
-
expect(key).toEqual(
|
|
73
|
-
expect.objectContaining({
|
|
74
|
-
key: "live_1OvRrfHbPdiCUrFAD4VwxiqEgg8L5uiVDlIgE4075juY7TnimZQG1Ll770irHyfM",
|
|
75
|
-
name: "my-new-api-key1",
|
|
76
|
-
env: "live",
|
|
77
|
-
})
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it("should update a key", async () => {
|
|
82
|
-
const client = createClient();
|
|
83
|
-
const key = await client.apiKeys.updateKey(
|
|
84
|
-
"live_TVHW0PVtktylIVObMd8J0sBHb7Ym3ZraObpeT3qxu7YRHig2KxrEIwggn50sBpSZ",
|
|
85
|
-
{
|
|
86
|
-
name: "my-first-update-key",
|
|
87
|
-
}
|
|
88
|
-
);
|
|
89
|
-
expect(key.name).toEqual("my-first-updated-key");
|
|
90
|
-
expect(key.key).toEqual(
|
|
91
|
-
"live_TVHW0PVtktylIVObMd8J0sBHb7Ym3ZraObpeT3qxu7YRHig2KxrEIwggn50sBpSZ"
|
|
92
|
-
);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("should delete a key", async () => {
|
|
96
|
-
const client = createClient();
|
|
97
|
-
const response = await client.apiKeys.deleteKey(
|
|
98
|
-
"live_TVHW0PVtktylIVObMd8J0sBHb7Ym3ZraObpeT3qxu7YRHig2KxrEIwggn50sBpSZ"
|
|
99
|
-
);
|
|
100
|
-
expect(response).toBeTruthy();
|
|
101
|
-
});
|
|
102
|
-
});
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
import TheAuthAPI from "../../index";
|
|
2
|
-
import testServer from "../testServer/server";
|
|
3
|
-
import { Server } from "http";
|
|
4
|
-
|
|
5
|
-
const port = 4063;
|
|
6
|
-
|
|
7
|
-
const createClient = (options?: any) => {
|
|
8
|
-
options = Object.assign(
|
|
9
|
-
{
|
|
10
|
-
host: `http://localhost:${port}`,
|
|
11
|
-
},
|
|
12
|
-
options
|
|
13
|
-
);
|
|
14
|
-
return new TheAuthAPI("access_key", options);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
describe("Projects", () => {
|
|
18
|
-
let server: Server;
|
|
19
|
-
beforeAll(() => {
|
|
20
|
-
server = testServer.listen(port);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
afterAll(() => {
|
|
24
|
-
server.close();
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("should list all projects", async () => {
|
|
28
|
-
const client = createClient();
|
|
29
|
-
const projects = await client.projects.getProjects("project-id-1");
|
|
30
|
-
expect(projects).toEqual(
|
|
31
|
-
expect.arrayContaining([
|
|
32
|
-
expect.objectContaining({
|
|
33
|
-
isActive: true,
|
|
34
|
-
createdBy: "user-id-1",
|
|
35
|
-
createdByType: "ACCESS_KEY",
|
|
36
|
-
updatedAt: new Date("2022-04-05T21:42:58.054Z"),
|
|
37
|
-
createdAt: new Date("2022-04-05T21:42:58.054Z"),
|
|
38
|
-
id: "project-id-1",
|
|
39
|
-
name: "My Auth Project",
|
|
40
|
-
accountId: "my-account-id",
|
|
41
|
-
env: "live",
|
|
42
|
-
}),
|
|
43
|
-
])
|
|
44
|
-
);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("should get a project using a projectId", async () => {
|
|
48
|
-
const client = createClient();
|
|
49
|
-
const project = await client.projects.getProject("project-id-2");
|
|
50
|
-
expect(project).toEqual(
|
|
51
|
-
expect.objectContaining({
|
|
52
|
-
isActive: true,
|
|
53
|
-
updatedAt: new Date("2022-04-02T21:02:14.808Z"),
|
|
54
|
-
createdAt: new Date("2022-04-02T21:02:14.808Z"),
|
|
55
|
-
id: "project-id-2",
|
|
56
|
-
name: "Hulu",
|
|
57
|
-
accountId: "my-account-id-2",
|
|
58
|
-
env: "live",
|
|
59
|
-
})
|
|
60
|
-
);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("should delete a project", async () => {
|
|
64
|
-
const client = createClient();
|
|
65
|
-
const project = await client.projects.deleteProject("my-project-id");
|
|
66
|
-
expect(project).toBeTruthy();
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it("should create a project", async () => {
|
|
70
|
-
const client = createClient();
|
|
71
|
-
const project = await client.projects.createProject(
|
|
72
|
-
"project-id-1",
|
|
73
|
-
"my-account-id"
|
|
74
|
-
);
|
|
75
|
-
expect(project).toEqual(
|
|
76
|
-
expect.objectContaining({
|
|
77
|
-
isActive: true,
|
|
78
|
-
updatedAt: new Date("2022-04-05T21:42:58.054Z"),
|
|
79
|
-
createdAt: new Date("2022-04-05T21:42:58.054Z"),
|
|
80
|
-
id: "project-id-1",
|
|
81
|
-
name: "My Auth Project",
|
|
82
|
-
accountId: "my-account-id",
|
|
83
|
-
env: "live",
|
|
84
|
-
})
|
|
85
|
-
);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it("should update a project", async () => {
|
|
89
|
-
const client = createClient();
|
|
90
|
-
const project = await client.projects.updateProject("project-id-1", {
|
|
91
|
-
name: "my-updated-project-1",
|
|
92
|
-
});
|
|
93
|
-
expect(project).toEqual(
|
|
94
|
-
expect.objectContaining({
|
|
95
|
-
isActive: true,
|
|
96
|
-
updatedAt: new Date("2022-04-05T21:42:58.054Z"),
|
|
97
|
-
createdAt: new Date("2022-04-05T21:42:58.054Z"),
|
|
98
|
-
id: "project-id-1",
|
|
99
|
-
name: "my-updated-project-1",
|
|
100
|
-
accountId: "my-account-id",
|
|
101
|
-
env: "live",
|
|
102
|
-
})
|
|
103
|
-
);
|
|
104
|
-
});
|
|
105
|
-
});
|
package/src/tests/index.spec.ts
DELETED
|
@@ -1,35 +0,0 @@
|
|
|
1
|
-
import TheAuthAPI from "../index";
|
|
2
|
-
import { Server } from "http";
|
|
3
|
-
import testServer from "./testServer/server";
|
|
4
|
-
|
|
5
|
-
const port = 4063;
|
|
6
|
-
|
|
7
|
-
const createClient = (options?: any) => {
|
|
8
|
-
options = Object.assign(
|
|
9
|
-
{
|
|
10
|
-
host: `http://localhost:${port}`,
|
|
11
|
-
},
|
|
12
|
-
options
|
|
13
|
-
);
|
|
14
|
-
return new TheAuthAPI("access_key", options);
|
|
15
|
-
};
|
|
16
|
-
|
|
17
|
-
describe("index", () => {
|
|
18
|
-
let server: Server;
|
|
19
|
-
beforeAll(() => {
|
|
20
|
-
server = testServer.listen(port);
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
afterAll(() => {
|
|
24
|
-
server.close();
|
|
25
|
-
});
|
|
26
|
-
it("should authenticate a valid key [legacy]", async () => {
|
|
27
|
-
const client = createClient();
|
|
28
|
-
const data: any = await client.authenticateAPIKey(
|
|
29
|
-
"aaa13d30-135e-11ec-8e0f-f1de8e89"
|
|
30
|
-
);
|
|
31
|
-
expect(data.customUserId).toEqual("my-user-id");
|
|
32
|
-
expect(data.customAccountId).toEqual("acc-id");
|
|
33
|
-
expect(data.authenticated).toBeTruthy();
|
|
34
|
-
}, 30000);
|
|
35
|
-
});
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
import {Request, Response, NextFunction} from "express"
|
|
2
|
-
import {version} from "../../../libraryMeta";
|
|
3
|
-
|
|
4
|
-
export function headersMiddleware(request: Request, response: Response, next: NextFunction) {
|
|
5
|
-
const accessKey = request.header("x-api-key");
|
|
6
|
-
if (!accessKey) {
|
|
7
|
-
return response.status(400).json({
|
|
8
|
-
message: "missing access key",
|
|
9
|
-
});
|
|
10
|
-
}
|
|
11
|
-
if (request.header("user-agent") !== `theauthapi-client-node/${version}`) {
|
|
12
|
-
return response.status(400).json({
|
|
13
|
-
message: "invalid user-agent",
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
next();
|
|
17
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { Router } from "express";
|
|
2
|
-
|
|
3
|
-
const router = Router();
|
|
4
|
-
|
|
5
|
-
export const apiKeyRoutes = router
|
|
6
|
-
.get("/:key", (request, response) => {
|
|
7
|
-
const key = request.params.key;
|
|
8
|
-
if (!key) {
|
|
9
|
-
return response.status(400).json({
|
|
10
|
-
message: "missing api-key",
|
|
11
|
-
});
|
|
12
|
-
}
|
|
13
|
-
return response.json({
|
|
14
|
-
key: "KGTSsxbDndjRRcpJGuQQp2or9UmQkqRrVQpCWgQruIXnvnNatmfdmOTcsgYnNwnH",
|
|
15
|
-
name: "My customers first Api Key",
|
|
16
|
-
customMetaData: {},
|
|
17
|
-
customAccountId: "acc-id",
|
|
18
|
-
env: "live",
|
|
19
|
-
createdAt: "2022-03-16T10:34:23.353Z",
|
|
20
|
-
updatedAt: "2022-03-16T10:34:23.353Z",
|
|
21
|
-
});
|
|
22
|
-
})
|
|
23
|
-
.get("/", (request, response) => {
|
|
24
|
-
const { projectId } = request.query;
|
|
25
|
-
if (!projectId) {
|
|
26
|
-
return response.status(400).json({
|
|
27
|
-
message: "missing project-id",
|
|
28
|
-
});
|
|
29
|
-
}
|
|
30
|
-
return response.json([
|
|
31
|
-
{
|
|
32
|
-
key: "live_h3uDZInxQexGLkwoxMDmuqz6PsyXGjkbrmSTpEwFb8l97mdAlQKtt14kt9Rv91PL",
|
|
33
|
-
name: "my-first-api-key",
|
|
34
|
-
customMetaData: {},
|
|
35
|
-
customAccountId: null,
|
|
36
|
-
env: "live",
|
|
37
|
-
createdAt: "2022-04-03T01:59:32.051Z",
|
|
38
|
-
updatedAt: "2022-04-03T01:59:32.051Z",
|
|
39
|
-
},
|
|
40
|
-
{
|
|
41
|
-
key: "live_1OvRrfHbPdiCUrFAD4VwxiqEgg8L5uiVDlIgE4075juY7TnimZQG1Ll770irHyfM",
|
|
42
|
-
name: "my-second-api-key",
|
|
43
|
-
customMetaData: {},
|
|
44
|
-
customAccountId: null,
|
|
45
|
-
env: "live",
|
|
46
|
-
createdAt: "2022-04-03T02:02:09.730Z",
|
|
47
|
-
updatedAt: "2022-04-03T02:02:09.730Z",
|
|
48
|
-
},
|
|
49
|
-
]);
|
|
50
|
-
})
|
|
51
|
-
.post("/", (request, response) => {
|
|
52
|
-
const { name, projectId } = request.body;
|
|
53
|
-
if (!name || !projectId) {
|
|
54
|
-
response.json({
|
|
55
|
-
message: "malformed apikey: missing name or projectId",
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
return response.json({
|
|
59
|
-
key: "live_1OvRrfHbPdiCUrFAD4VwxiqEgg8L5uiVDlIgE4075juY7TnimZQG1Ll770irHyfM",
|
|
60
|
-
name: "my-new-api-key1",
|
|
61
|
-
env: "live",
|
|
62
|
-
createdAt: "2022-04-02T21:10:39.648Z",
|
|
63
|
-
updatedAt: "2022-04-02T21:10:39.648Z",
|
|
64
|
-
});
|
|
65
|
-
})
|
|
66
|
-
.patch("/:key", (request, response) => {
|
|
67
|
-
const { key } = request.params;
|
|
68
|
-
if (!key) {
|
|
69
|
-
return response.status(400).json({
|
|
70
|
-
message: "missing api-key",
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
return response.json({
|
|
74
|
-
key: "live_TVHW0PVtktylIVObMd8J0sBHb7Ym3ZraObpeT3qxu7YRHig2KxrEIwggn50sBpSZ",
|
|
75
|
-
name: "my-first-updated-key",
|
|
76
|
-
env: "live",
|
|
77
|
-
createdAt: "2022-04-02T21:10:39.648Z",
|
|
78
|
-
updatedAt: "2022-04-02T21:10:39.648Z",
|
|
79
|
-
});
|
|
80
|
-
})
|
|
81
|
-
.delete("/:key", (request, response) => {
|
|
82
|
-
const { key } = request.params;
|
|
83
|
-
if (!key) {
|
|
84
|
-
return response.status(400).json({
|
|
85
|
-
message: "missing api-key",
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
return response.json(true);
|
|
89
|
-
});
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { Router } from "express";
|
|
2
|
-
|
|
3
|
-
const router = Router();
|
|
4
|
-
|
|
5
|
-
export const authRoutes = router.post("/authenticate", (request, response) => {
|
|
6
|
-
const { credentials } = request.body;
|
|
7
|
-
if (!credentials || !credentials.api_key) {
|
|
8
|
-
return response.json({
|
|
9
|
-
message: "missing api_key",
|
|
10
|
-
});
|
|
11
|
-
}
|
|
12
|
-
if (credentials.api_key.length !== 32) {
|
|
13
|
-
return response.json({
|
|
14
|
-
message: "api_key must have a length of 32",
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
return response.json({
|
|
19
|
-
customMetaData: {},
|
|
20
|
-
customAccountId: "acc-id",
|
|
21
|
-
customUserId: "my-user-id",
|
|
22
|
-
authenticated: true,
|
|
23
|
-
});
|
|
24
|
-
});
|