Skip to content

Commit

Permalink
Merge pull request #4 from symblai/access-token-patch
Browse files Browse the repository at this point in the history
Access token patch
  • Loading branch information
Adam Voliva authored Sep 7, 2021
2 parents a5f97dc + 7abd07d commit bea5c92
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 53 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@symblai/symbl-js",
"version": "1.1.3a",
"version": "1.1.3c",
"description": "Javascript SDK for Symbl.ai's Language Insights API Platform",
"main": "build/app.bundle.js",
"dependencies": {
Expand Down
19 changes: 11 additions & 8 deletions src/ClientSDK.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ export default class ClientSDK {
throw new Error('options with appId and appSecret must be provided.');
}

const {appId, appSecret, logLevel, tlsAuth, basePath} = await options;
const {appId, appSecret, logLevel, tlsAuth, basePath, accessToken} = await options;

if (!appId) {
if (!appId && !accessToken) {
throw new Error('appId is required.');
}

if (!appSecret) {
if (!appSecret && !accessToken) {
throw new Error('appSecret is required.');
}

Expand All @@ -44,9 +44,8 @@ export default class ClientSDK {
this.basePath = basePath;

logger.trace('Initializing SDK with options: ', options);

return new Promise((resolve, reject) => {
this.oauth2.init(options.appId, options.appSecret)
const onInitPromise = (resolve, reject) => {
this.oauth2.init(options.appId, options.appSecret, options.accessToken)
.then(() => {
const apiClient = new ApiClient();
if (basePath || (basePath && basePath !== this.oauth2.apiClient.basePath)) {
Expand All @@ -58,7 +57,12 @@ export default class ClientSDK {
this.endpointClient = new EndpointApi({}, apiClient);
resolve();
}).catch(reason => reject(reason));
});
};
if (!accessToken) {
return new Promise(onInitPromise);
} else {
return new Promise(onInitPromise);
}
}

async startRealtimeRequest(options = {}) {
Expand All @@ -67,7 +71,6 @@ export default class ClientSDK {
}

options.basePath = options.basePath || this.basePath;

const realtimeClient = new RealtimeApi(options, this.oauth2);

const startRequest = (resolve, reject) => {
Expand Down
1 change: 0 additions & 1 deletion src/api/EndpointApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ export default class EndpointApi {
}

const endpointConnectRequest = EndpointConnectRequest.constructFromObject(request);

return new Promise((resolve, reject) => {
try {
this.connectionToEndpointApi.connectToEndpoint(endpointConnectRequest, (error, data, response) => {
Expand Down
100 changes: 57 additions & 43 deletions src/api/OAuth2.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,59 +66,73 @@ export default class OAuth2 {
}
}

init(appId, appSecret) {
init(appId, appSecret, appToken) {
if (arguments.length < 2) {
throw new Error(`Expected number of arguments 2, detected: ${arguments.length}`);
}

if (!appId) {
throw new Error('appId is required.');
}
if (appToken) {
return new Promise((resolve, reject) => {
this.activeToken = appToken;
logger.trace('Token received.');
this.apiClient.authentications.jwt.apiKey = this.activeToken;
resolve({
accessToken: appToken
})
});
} else {

if (!appSecret) {
throw new Error('appSecret is required.');
}
if (!appId) {
throw new Error('appId is required.');
}

if (!appSecret) {
throw new Error('appSecret is required.');
}

this.appId = appId;
this.appSecret = appSecret;

return new Promise((resolve, reject) => {
logger.trace(`Initializing app with appId and appSecret`, appId, appSecret);
try {
const grant = Grant.constructFromObject({
type: 'application',
appId,
appSecret
});
this.authenticationApi.generateToken(grant, (err, data) => {
if (err) {
if (err.status && err.status === 401) {
const message = 'Combination of appId and appSecret is not valid.';
logger.info(message);
reject({
message,
internalError: err
this.appId = appId;
this.appSecret = appSecret;

return new Promise((resolve, reject) => {
logger.trace(`Initializing app with appId and appSecret`, appId, appSecret);
try {
const grant = Grant.constructFromObject({
type: 'application',
appId,
appSecret
});
this.authenticationApi.generateToken(grant, (err, data) => {
if (err) {
if (err.status && err.status === 401) {
const message = 'Combination of appId and appSecret is not valid.';
logger.info(message);
reject({
message,
internalError: err
});
} else {
reject(ErrorHandler.getError(err));
}
} else if (data) {
this.processTokenResult(data);
const {accessToken, expiresIn} = data;
resolve({
accessToken: accessToken,
expiresIn: expiresIn
});

} else {
reject(ErrorHandler.getError(err));
reject(ErrorHandler.getError());
}
} else if (data) {
this.processTokenResult(data);
const {accessToken, expiresIn} = data;
resolve({
accessToken: accessToken,
expiresIn: expiresIn
});

} else {
reject(ErrorHandler.getError());
}
});
} catch (e) {
reject(ErrorHandler.getError(e));
}
});
} catch (e) {
reject(ErrorHandler.getError(e));
}

});

}

});
}


Expand Down

0 comments on commit bea5c92

Please sign in to comment.