Skip to content

Commit

Permalink
Add VerifyBotAuth
Browse files Browse the repository at this point in the history
  • Loading branch information
sombochea committed May 10, 2024
1 parent fd076e2 commit 486691c
Show file tree
Hide file tree
Showing 8 changed files with 272 additions and 15 deletions.
38 changes: 33 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

A simple way to send and verify verification code with SMS, Email, Telegram and etc.

- [x] Send verify code
- [x] Verify a code
- [x] Send verify code.
- [x] Verify a code.
- [x] VerifyBotAuth.

## Supported Providers

Expand All @@ -13,11 +14,12 @@ A simple way to send and verify verification code with SMS, Email, Telegram and

## Usages

- Create a verify message and send it to the target
### Verify Feature

- Create a verify message and send it to the target.

```typescript
const API_KEY = 'vm_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ';

const sdk = Verifyme.create(VerifymeOptions.builder().apiKey(API_KEY).build());

const request = CreateVerifyRequest.builder()
Expand All @@ -31,7 +33,7 @@ console.log('Request: ', request);
console.log('Response: ', response);
```

- Verify the message
- Verify the message.

```typescript
const request = VerifyMessageRequest.builder()
Expand All @@ -44,6 +46,32 @@ console.log('Request: ', request);
console.log('Response: ', response);
```

### VerifyBotAuth Feature

- Create an auth state request with the target.

```typescript
const API_KEY = 'vm_gX9WwSdKatMNdpUClLU0IfCx575tvdoeQ';
const sdk = Verifyme.create(VerifymeOptions.builder().apiKey(API_KEY).build());

const request: VerifyBotAuthCreate = {
target: '+855769995149',
type: 'telegram_bot',
};

const response = await sdk.botAuth().auth(request);
console.log('Request: ', request);
console.log('Response: ', response);
```

- Get the auth state.

```typescript
const result = await sdk.botAuth().state(response.state!);
console.log('State: ', state);
console.log('Result: ', result);
```

### Contributors

- Sambo Chea <sombochea@cubetiqs.com>
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cubetiq/verifyme",
"version": "0.0.4",
"version": "0.1.5",
"description": "A simple way to send and verify verification code with SMS, Email, Telegram and etc.",
"main": "dist/index.js",
"private": false,
Expand Down
68 changes: 65 additions & 3 deletions src/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ enum Provider {

type ProviderType = 'telegram' | 'verifybot' | 'sms' | 'email';

export type VerifyBotAuthType = 'telegram_bot' | 'qr_code';
export type VerifyBotAuthStatus = 'pending' | 'verified' | 'expired' | 'rejected';

class CreateVerifyRequest {
provider?: Provider | ProviderType | string; // telegram, verifybot, sms, email
target?: string; // phone, email, telegram chat id, etc.
Expand Down Expand Up @@ -41,7 +44,7 @@ class CreateVerifyRequestBuilder {
private _timeout?: number;
private _template?: string;

constructor() {}
constructor() { }

provider(
provider: Provider | ProviderType | string | undefined
Expand Down Expand Up @@ -113,7 +116,7 @@ class VerifyMessageRequestBuilder {
private _token?: string;
private _code?: string;

constructor() {}
constructor() { }

token(token: string | undefined): VerifyMessageRequestBuilder {
this._token = token;
Expand Down Expand Up @@ -182,7 +185,7 @@ class VerifymeOptionsBuilder {
private _apiKey?: string;
private _connectionTimeout?: number;

constructor() {}
constructor() { }

url(url: string | undefined): VerifymeOptionsBuilder {
this._url = url;
Expand Down Expand Up @@ -210,6 +213,65 @@ class VerifymeOptionsBuilder {
}
}

export class VerifyBotAuthCreate {
target?: string;
type?: VerifyBotAuthType | string;

constructor({
target,
type,
}: {
target?: string;
type?: VerifyBotAuthType | string;
}) {
this.target = target;
this.type = type;
}

static builder(): VerifyBotAuthCreateBuilder {
return new VerifyBotAuthCreateBuilder();
}
}

export class VerifyBotAuthCreateBuilder {
private _target?: string;
private _type?: VerifyBotAuthType | string;

constructor() { }

target(target: string | undefined): VerifyBotAuthCreateBuilder {
this._target = target;
return this;
}

type(type: VerifyBotAuthType | string | undefined): VerifyBotAuthCreateBuilder {
this._type = type;
return this;
}

build(): VerifyBotAuthCreate {
return new VerifyBotAuthCreate({
target: this._target,
type: this._type,
});
}
}

export interface VerifyBotAuthCreated {
state?: string;
exp?: number;
link?: string;
qr_link?: string;
error?: string;
}

export interface VerifyBotAuthGetState {
target?: string;
status?: VerifyBotAuthStatus | string;
data?: any;
error?: string;
}

export {
Provider,
ProviderType,
Expand Down
83 changes: 83 additions & 0 deletions src/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,89 @@ class VerifymeService {
};
});
}

///////// VerifyBotAuth /////////
async createBotAuth(
body: any,
headers?: any,
timeout?: number
): Promise<CreateVerifyResponse> {
const config: AxiosRequestConfig = {
method: 'POST',
url: `${this.url}/botauth`,
data: body,
headers: headers,
timeout: timeout ? timeout * 1000 : undefined,
};

return axios(config)
.then((response) => response.data)
.catch((error) => {
if (error.response) {
console.debug(
`CreateBotAuth Error: ${JSON.stringify(
error.response.data
)}`
);

return {
...error.response.data,
};
} else if (error.request) {
console.debug(
`CreateBotAuth Error: ${JSON.stringify(error.request)}`
);

return {
error: error.request,
};
}

return {
error: error?.message ?? 'Unknown error',
};
});
}

async getBotAuth(
state: string,
headers?: any,
timeout?: number
): Promise<VerifyMessageResponse> {
const config: AxiosRequestConfig = {
method: 'GET',
url: `${this.url}/botauth/${state}`,
headers: headers,
timeout: timeout ? timeout * 1000 : undefined,
};

return axios(config)
.then((response) => response.data)
.catch((error) => {
if (error.response) {
console.debug(
`GetBotAuth Error: ${JSON.stringify(error.response.data)}`
);

return {
...error.response.data,
};
} else if (error.request) {
console.debug(
`GetBotAuth Error: ${JSON.stringify(error.request)}`
);

return {
error: error.request,
};
}

return {
error: error?.message ?? 'Unknown error',
};
});
}
///////// VerifyBotAuth /////////
}

export { VerifymeService };
29 changes: 29 additions & 0 deletions src/verifybotauth.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { VerifyBotAuthCreate, VerifyBotAuthCreated, VerifyBotAuthGetState } from "./model";
import { VerifymeService } from "./service";

export class VerifyBotAuth {
private readonly _logger = console;
private readonly _service: VerifymeService;

constructor(service: VerifymeService) {
this._service = service;
}

async auth(request: VerifyBotAuthCreate): Promise<VerifyBotAuthCreated> {
this._logger.info(`[VerifyBotAuth] Creating auth with target: ${request.target}`);

const response = await this._service.createBotAuth(request);
return response;
}

async state(state: string): Promise<VerifyBotAuthGetState> {
if (!state) {
throw new Error('State is required');
}

this._logger.info(`[VerifyBotAuth] Getting auth state with state: ${state}`);

const response = await this._service.getBotAuth(state);
return response;
}
}
15 changes: 12 additions & 3 deletions src/verifyme.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { CreateVerifyRequest, CreateVerifyResponse, VerifyMessageRequest, VerifyMessageResponse, VerifymeOptions } from "./model";
import { VerifymeService } from "./service";
import { getSystemHostname, getSystemUsername } from "./util";
import { VerifyBotAuth } from "./verifybotauth";

export class Verifyme {
private static readonly _logger = console;
private static readonly NAME = 'verifyme';
private static readonly VERSION = '0.0.4';
private static readonly VERSION_CODE = '2';
private static readonly VERSION = '0.1.5';
private static readonly VERSION_CODE = '3';
private static readonly DEFAULT_URL = 'https://verifyme-api.cubetiq.app';
private static readonly API_KEY_HEADER_PREFIX = 'x-api-key';
private static readonly DEFAULT_CONNECT_TIMEOUT = 60; // seconds

private _options!: VerifymeOptions;
private _service!: VerifymeService;
private _botAuth!: VerifyBotAuth;

constructor(options: VerifymeOptions) {
if (!options.apiKey) {
Expand All @@ -26,6 +28,9 @@ export class Verifyme {
// Initialize service
this._service = new VerifymeService(this._options.url);

// Initialize bot auth
this._botAuth = new VerifyBotAuth(this._service);

Verifyme._logger.log(`[Verifyme] Initialized SDK Version: ${Verifyme.VERSION}-${Verifyme.VERSION_CODE}`);
}

Expand All @@ -37,7 +42,7 @@ export class Verifyme {
const sender = getSystemUsername() ?? 'unknown';

// User agent to request
const userAgent = `verifyme-sdk-node/${Verifyme.VERSION}-${Verifyme.VERSION_CODE} (${hostname}:${sender})`;
const userAgent = `${Verifyme.NAME}-sdk-node/${Verifyme.VERSION}-${Verifyme.VERSION_CODE} (${hostname}:${sender})`;
Verifyme._logger.info(`[Verifyme] User agent: ${userAgent}`);

const headers: Record<string, string> = {
Expand Down Expand Up @@ -71,6 +76,10 @@ export class Verifyme {
return response;
}

botAuth(): VerifyBotAuth {
return this._botAuth;
}

static create(options: VerifymeOptions): Verifyme {
return new Verifyme(options);
}
Expand Down
Loading

0 comments on commit 486691c

Please sign in to comment.