mirror of
https://github.com/Tribufu/TribufuJS
synced 2026-08-18 00:51:07 +00:00
Bearer token auth draft
This commit is contained in:
parent
26dbeb6867
commit
43b5a53520
4 changed files with 56 additions and 8 deletions
|
|
@ -1,4 +1,5 @@
|
||||||
NODE_ENV="development"
|
NODE_ENV="development"
|
||||||
|
TRIBUFU_ACCESS_TOKEN=""
|
||||||
TRIBUFU_API_KEY=""
|
TRIBUFU_API_KEY=""
|
||||||
TRIBUFU_API_URL="https://api.tribufu.com"
|
TRIBUFU_API_URL="https://api.tribufu.com"
|
||||||
TRIBUFU_CLIENT_ID=""
|
TRIBUFU_CLIENT_ID=""
|
||||||
|
|
|
||||||
15
examples/bearer.js
Normal file
15
examples/bearer.js
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
// SPDX-License-Identifier: MIT AND Apache-2.0
|
||||||
|
|
||||||
|
import dotenv from "dotenv";
|
||||||
|
import { TribufuApi } from "../build/index.mjs";
|
||||||
|
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const accessToken = process.env["TRIBUFU_ACCESS_TOKEN"];
|
||||||
|
const tribufu = TribufuApi.withAccessToken(accessToken);
|
||||||
|
console.log(await tribufu.getUserInfo());
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
12
src/credentials.ts
Normal file
12
src/credentials.ts
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
// Copyright (c) Tribufu. All Rights Reserved.
|
||||||
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
|
export enum CredentialsType {
|
||||||
|
ApiKey,
|
||||||
|
Bearer,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface Credentials {
|
||||||
|
type: CredentialsType;
|
||||||
|
value: string;
|
||||||
|
}
|
||||||
36
src/index.ts
36
src/index.ts
|
|
@ -2,6 +2,7 @@
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
import { Configuration } from "./runtime";
|
import { Configuration } from "./runtime";
|
||||||
|
import { Credentials, CredentialsType } from "./credentials";
|
||||||
import { JavaScriptRuntime } from "./node";
|
import { JavaScriptRuntime } from "./node";
|
||||||
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from "./constants";
|
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from "./constants";
|
||||||
import { TribufuGeneratedApi } from "./apis/TribufuGeneratedApi";
|
import { TribufuGeneratedApi } from "./apis/TribufuGeneratedApi";
|
||||||
|
|
@ -20,12 +21,12 @@ export * from "./singletion";
|
||||||
*/
|
*/
|
||||||
export class TribufuApi extends TribufuGeneratedApi {
|
export class TribufuApi extends TribufuGeneratedApi {
|
||||||
/**
|
/**
|
||||||
* Create a TribufuApi with the given API key.
|
* Create a TribufuApi with the credentials.
|
||||||
*
|
*
|
||||||
* @param apiKey The API key for authentication.
|
* @param credentials The credentials for authentication.
|
||||||
*/
|
*/
|
||||||
constructor(apiKey: string | null = null) {
|
constructor(credentials: Credentials | null = null) {
|
||||||
super(TribufuApi.createConfiguration(apiKey));
|
super(TribufuApi.createConfiguration(credentials));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -46,7 +47,19 @@ export class TribufuApi extends TribufuGeneratedApi {
|
||||||
* @returns TribufuApi
|
* @returns TribufuApi
|
||||||
*/
|
*/
|
||||||
public static withApiKey(apiKey: string): TribufuApi {
|
public static withApiKey(apiKey: string): TribufuApi {
|
||||||
return new TribufuApi(apiKey);
|
return new TribufuApi({ type: CredentialsType.ApiKey, value: apiKey });
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a TribufuApi with the given access token.
|
||||||
|
*
|
||||||
|
* - A access token give you user read and write access to the Tribufu API.
|
||||||
|
*
|
||||||
|
* @param accessToken
|
||||||
|
* @returns TribufuApi
|
||||||
|
*/
|
||||||
|
public static withAccessToken(accessToken: string): TribufuApi {
|
||||||
|
return new TribufuApi({ type: CredentialsType.Bearer, value: accessToken });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -96,14 +109,21 @@ export class TribufuApi extends TribufuGeneratedApi {
|
||||||
/**
|
/**
|
||||||
* Creates a configuration for the Tribufu API client.
|
* Creates a configuration for the Tribufu API client.
|
||||||
*/
|
*/
|
||||||
private static createConfiguration(apiKey: string | null): Configuration {
|
private static createConfiguration(credentials: Credentials | null): Configuration {
|
||||||
const basePath = this.getBaseUrl();
|
const basePath = this.getBaseUrl();
|
||||||
const headers: Record<string, string> = {
|
const headers: Record<string, string> = {
|
||||||
"User-Agent": this.getUserAgent(),
|
"User-Agent": this.getUserAgent(),
|
||||||
};
|
};
|
||||||
|
|
||||||
if (apiKey) {
|
if (credentials) {
|
||||||
headers["Authorization"] = `ApiKey ${apiKey}`;
|
switch (credentials.type) {
|
||||||
|
case CredentialsType.ApiKey:
|
||||||
|
headers["Authorization"] = `ApiKey ${credentials.value}`;
|
||||||
|
break;
|
||||||
|
case CredentialsType.Bearer:
|
||||||
|
headers["Authorization"] = `Bearer ${credentials.value}`;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new Configuration({
|
return new Configuration({
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue