Skip to content

Commit

Permalink
Merge pull request #32 from telstra/maintain/MessagingAPI-V3-SDK
Browse files Browse the repository at this point in the history
release 3.0.6
  • Loading branch information
zhanganderson authored Aug 23, 2024
2 parents 6198f47 + 671d95c commit c09f1bd
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "3.0.5",
"version": "3.0.6",
"license": "Apache-2.0",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
Expand Down
46 changes: 25 additions & 21 deletions src/messaging/classes/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,38 @@ export abstract class HttpClient {
config.headers['Accept-Charset'] = `utf-8`;
config.headers['Accept'] = `application/json`;
config.headers['Content-Type'] = `application/json`;
//config.headers['content-type'] = `application/json`;
}

if (config.url !== '/v2/oauth/token') {
// check token validity
const access_token = await checkTokenValidity();

let access_token = await checkTokenValidity();
if (access_token) {
config.headers['Authorization'] = `Bearer ${access_token}`;
config.headers['Authorization'] = `Bearer ${access_token}`;
} else {
// retrieve auth credentials
const authCredentials = await this.auth.getCredentials();

// request new token
const { access_token } = await this.renewToken(authCredentials);

// request new token
let renewed_token = await this.renewToken(authCredentials);
// set token if valid
if (access_token) {
if (renewed_token) {
// set authorization headers
config.headers['Authorization'] = `Bearer ${access_token}`;
config.headers['Authorization'] = `Bearer ${renewed_token}`;
// set authorization token in storage
const futureTimeStamp = addMinutes(
new Date(),
Constants.TOKEN_EXPIRE_IN_50_MINS
);
await setAuthToken(
access_token,
renewed_token,
futureTimeStamp.toISOString()
);
}
}
}


return config;
};

Expand Down Expand Up @@ -132,16 +131,16 @@ export abstract class HttpClient {
const authCredentials = await this.auth.getCredentials();

// request new token
const { access_token } = await this.renewToken(authCredentials);
let renewed_token = await this.renewToken(authCredentials);

// set token if valid
if (access_token) {
if (renewed_token) {
// set authorization token in storage
const futureTimeStamp = addMinutes(
new Date(),
Constants.TOKEN_EXPIRE_IN_50_MINS
);
await setAuthToken(access_token, futureTimeStamp.toISOString());
await setAuthToken(renewed_token, futureTimeStamp.toISOString());

return this.instance(
originalRequest as InternalAxiosRequestConfig
Expand Down Expand Up @@ -169,9 +168,7 @@ export abstract class HttpClient {
return Promise.reject(error);
};

private async renewToken(
authCredentials: AuthCredentials
): Promise<{ access_token: string }> {
private async renewToken(authCredentials: AuthCredentials): Promise<string | null> {
const params = new URLSearchParams();
params.append('client_id', `${authCredentials.client_id}`);
params.append('client_secret', `${authCredentials.client_secret}`);
Expand All @@ -180,10 +177,17 @@ export abstract class HttpClient {
'scope',
'free-trial-numbers:read free-trial-numbers:write messages:read messages:write virtual-numbers:read virtual-numbers:write reports:read reports:write'
);

const auth = await this.instance.post(`/v2/oauth/token`, params);
if (!auth) return auth;
const { access_token } = auth;
return access_token;

try {
const auth = await this.instance.post(`/v2/oauth/token`, params);

if (auth && auth.access_token) {
return auth.access_token;
} else {
return null;
}
} catch (error) {
return null;
}
}
}
17 changes: 12 additions & 5 deletions src/messaging/utils/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,20 @@ export const getAuthToken = async (): Promise<

export const checkTokenValidity = async (): Promise<string | null> => {
try {
const authData = await getAuthToken();
const { accessToken, timeExp } = authData as { accessToken: string; timeExp: string };
const authData = await getAuthToken();

if (authData) {
let { accessToken, timeExp } = authData as { accessToken: string; timeExp: string };
let timeExpTimestamp: number = 0;
if ((timeExp !== '') && (accessToken !== '')) {
timeExpTimestamp = Number(timeExp);
}
else {
return null;
}

const timeExpTimestamp = Number(timeExp);
const currentTimeStamp = getTime(new Date());
const currentTimeStamp = getTime(new Date());

if (accessToken && timeExp) {
if (currentTimeStamp < timeExpTimestamp) {
// Token is still valid
return accessToken;
Expand Down

0 comments on commit c09f1bd

Please sign in to comment.