Parse bot token to get id and add examples

This commit is contained in:
Guilherme Werner
2024-01-01 00:41:40 -03:00
parent 5047dada7c
commit de1f90c622
14 changed files with 298 additions and 146 deletions

View File

@ -3,12 +3,13 @@
import { HeaderMap } from "./http";
import { JavaScriptRuntime } from "./node";
import { TribufuApiOptions } from "./options";
import { TribufuBot } from "./bot";
import { TribufuClient } from "./client";
import { TribufuServer } from "./server";
import axios, { AxiosInstance } from "axios";
export const TRIBUFU_VERSION = "0.0.0";
export const TRIBUFU_API_URL = "https://api.tribufu.com";
import jwt from "jsonwebtoken";
import { TokenPayload } from "./token";
import { TRIBUFU_API_URL, TRIBUFU_VERSION } from ".";
/**
* **Tribufu API**
@ -42,6 +43,10 @@ export class TribufuApi {
this.http = http;
}
/**
* Create a TribufuApi with the default options.
* @returns
*/
public static default(): TribufuApi {
return new TribufuApi({});
}
@ -207,6 +212,26 @@ export class TribufuApi {
return TribufuApi.detectRuntime() === JavaScriptRuntime.Node;
}
/**
* Extract the payload from a Tribufu token.
* @param token
* @returns TokenPayload | null
*/
protected static parseToken(token: string): TokenPayload | null {
try {
const payload = jwt.decode(token);
if (!payload) {
return null;
}
return payload as TokenPayload;
}
catch (error) {
return null;
}
}
/**
* Get current headers with the api key or access token.
* @returns HeaderMap
@ -403,41 +428,4 @@ export class TribufuApi {
return responseBody;
}
}
/**
* **Tribufu Bot**
*
* To authenticate a bot you need to use the bot token obtained from the Tribufu Developer Portal.
*
* - A bot is a special type of user account.
* - A bot give you read and write access to the Tribufu API.
*/
export class TribufuBot extends TribufuApi {
constructor(token: string) {
super({ accessToken: token });
}
/**
* Try to create a TribufuBot from environment variables.
*
* - This will only work if the environment variables are set.
*
* @param prefix A prefix for the environment variables.
* @returns TribufuBot | null
* @example
* ```ts
* const bot = TribufuBot.fromEnv("TRIBUFU_"); // process.env.TRIBUFU_BOT_TOKEN
* ```
*/
public static override fromEnv(prefix: string = ""): TribufuBot | null {
const token = process.env[`${prefix}BOT_TOKEN`];
if (token) {
return TribufuApi.withBot(token);
}
return null;
}
}

View File

@ -11,8 +11,26 @@ import { TribufuApi } from "./api";
* - A bot give you read and write access to the Tribufu API.
*/
export class TribufuBot extends TribufuApi {
private readonly botId: string;
constructor(token: string) {
const payload = TribufuApi.parseToken(token);
if (!payload) {
throw new Error("Invalid token");
}
if (payload.type !== "bot") {
throw new Error("Invalid token type");
}
if (!payload.bot_id) {
throw new Error("Invalid token payload");
}
super({ accessToken: token });
this.botId = payload.bot_id;
}
/**
@ -37,4 +55,12 @@ export class TribufuBot extends TribufuApi {
return null;
}
/**
* Get the bot id.
* @returns string
*/
public getBotId(): string {
return this.botId;
}
}

View File

@ -79,20 +79,37 @@ export class TribufuClient extends TribufuApi {
return client;
}
/**
* Set the tokens.
* @param accessToken
* @param refreshToken
* @param expiresIn
*/
private setTokens(accessToken: string | null, refreshToken?: string | null, expiresIn?: number | null): void {
this.options.accessToken = accessToken;
this.options.refreshToken = refreshToken || null;
this.options.expiresIn = expiresIn || null;
}
/**
* Clear the tokens.
*/
private clearTokens(): void {
this.setTokens(null, null, null);
}
/**
* Set the tokens from a oauth2 response.
* @param tokens
*/
private setTokensFromResponse(tokens: OAuth2TokenResponse): void {
this.setTokens(tokens.access_token, tokens.refresh_token || null, tokens.expires_in || null);
}
/**
* Get the headers for a oauth2 request.
* @returns HeaderMap
*/
private getOAuthHeaders(): HeaderMap {
let headers = this.getHeaders();
headers["Authorization"] = `Basic ${Buffer.from(`${this.clientId}:${this.clientSecret}`, "binary").toString("base64")}`;
@ -100,10 +117,18 @@ export class TribufuClient extends TribufuApi {
return headers;
}
/**
* Get the client id.
* @returns string
*/
public getClientId(): string {
return this.clientId;
}
/**
* Login using an authorization code.
* @param authorizationCode
* @returns
* @returns boolean
*/
public async authorizationLogin(authorizationCode: string): Promise<boolean> {
const response = await this.getToken("authorization_code", authorizationCode, null, null);
@ -120,7 +145,7 @@ export class TribufuClient extends TribufuApi {
/**
* Login using a device code.
* @param deviceCode
* @returns
* @returns boolean
*/
public async deviceLogin(deviceCode: string): Promise<boolean> {
const response = await this.getToken("device_code", deviceCode, null, null);
@ -138,7 +163,7 @@ export class TribufuClient extends TribufuApi {
* Login using a username and password.
* @param username
* @param password
* @returns
* @returns boolean
*/
public async passwordLogin(username: string, password: string): Promise<boolean> {
const response = await this.getToken("password", password, null, username);
@ -156,7 +181,7 @@ export class TribufuClient extends TribufuApi {
* Login using a passkey.
* @param username
* @param passkey
* @returns
* @returns boolean
*/
public async passkeyLogin(username: string, passkey: string): Promise<boolean> {
const response = await this.getToken("passkey", passkey, null, username);
@ -174,7 +199,7 @@ export class TribufuClient extends TribufuApi {
* Get a token for a client application.
* @param subjectKey
* @param subjectValue
* @returns
* @returns boolean
*/
public async clientLogin(subjectKey: string | null, subjectValue: string | null): Promise<boolean> {
const response = await this.getToken("client_credentials", null, subjectKey, subjectValue);
@ -191,7 +216,7 @@ export class TribufuClient extends TribufuApi {
/**
* Get a new access token using a refresh token.
* @param refreshToken
* @returns
* @returns boolean
*/
public async refreshToken(): Promise<boolean> {
if (!this.options.refreshToken) {
@ -211,7 +236,7 @@ export class TribufuClient extends TribufuApi {
/**
* Get information about a access token.
* @returns
* @returns boolean
*/
public async instrospectToken(): Promise<OAuth2IntrospectionResponse | null> {
if (!this.options.accessToken) {
@ -236,7 +261,7 @@ export class TribufuClient extends TribufuApi {
/**
* Revoke a refresh token.
* @returns
* @returns boolean
*/
public async revokeToken(): Promise<boolean> {
if (!this.options.refreshToken) {
@ -261,6 +286,10 @@ export class TribufuClient extends TribufuApi {
return true;
}
/**
* Check if the current access token is valid.
* @returns boolean
*/
public async isTokenValid(): Promise<boolean> {
const response = await this.instrospectToken();
@ -277,7 +306,7 @@ export class TribufuClient extends TribufuApi {
* @param grantValue
* @param subjectKey
* @param subjectValue
* @returns
* @returns OAuth2TokenResponse | null
*/
protected async getToken(grantType: OAuth2GrantType, grantValue: string | null, subjectKey: string | null, subjectValue: string | null): Promise<OAuth2TokenResponse | null> {
const requestBody: OAuth2TokenRequest = {
@ -305,7 +334,7 @@ export class TribufuClient extends TribufuApi {
/**
* Get information about the current user.
* @returns
* @returns User | null
*/
public async getUserInfo(): Promise<User | null> {
if (!this.options.refreshToken) {

View File

@ -47,6 +47,14 @@ export class TribufuServer extends TribufuClient {
return null;
}
/**
* Get the server id.
* @returns string
*/
public getServerId(): string {
return this.serverId;
}
/**
* Get a list of connected users.
* @returns

19
src/token.ts Normal file
View File

@ -0,0 +1,19 @@
// Copyright (c) Tribufu. All Rights Reserved.
export type TokenType = "user" | "bot" | "client" | "server";
export interface TokenPayload {
jti: string;
type: TokenType;
iss: string;
aud: string;
client_id: string;
scope: string;
user_id?: string;
bot_id?: string;
private_flags?: string;
public_flags?: string;
server_id?: string;
iat: number;
exp: number;
}