Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getTokenStatus method #838

Merged
merged 21 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion libraries/botbuilder/src/botFrameworkAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

import { Activity, ActivityTypes, BotAdapter, ChannelAccount, ConversationAccount, ConversationParameters, ConversationReference, ConversationsResult, IUserTokenProvider, ResourceResponse, TokenResponse, TurnContext } from 'botbuilder-core';
import { ChannelValidation, ConnectorClient, EmulatorApiClient, GovernmentConstants, JwtTokenValidation, MicrosoftAppCredentials, SimpleCredentialProvider, TokenApiClient, TokenApiModels } from 'botframework-connector';
import { ChannelValidation, ConnectorClient, EmulatorApiClient, GovernmentConstants, JwtTokenValidation, MicrosoftAppCredentials, SimpleCredentialProvider, TokenApiClient, TokenStatus, TokenApiModels } from 'botframework-connector';
import * as os from 'os';

/**
Expand Down Expand Up @@ -434,6 +434,27 @@ export class BotFrameworkAdapter extends BotAdapter implements IUserTokenProvide
return (await client.botSignIn.getSignInUrl(finalState, { channelId: context.activity.channelId }))._response.bodyAsText;
}

/**
* Retrieves the token status for each configured connection for the given user.
* @param context Context for the current turn of conversation with the user.
* @param userId The user Id for which token status is retrieved.
* @param includeFilter Optional comma seperated list of connection's to include. Blank will return token status for all configured connections.
* @returns Array of TokenStatus
* */

public async getTokenStatus(context: TurnContext, userId?: string, includeFilter?: string ): Promise<TokenStatus[]>
{
if (!userId && (!context.activity.from || !context.activity.from.id)) {
throw new Error(`BotFrameworkAdapter.getTokenStatus(): missing from or from.id`);
}
this.checkEmulatingOAuthCards(context);
userId = userId || context.activity.from.id;
const url: string = this.oauthApiUrl(context);
const client: TokenApiClient = this.createTokenApiClient(url);

return (await client.userToken.getTokenStatus(userId, {channelId: context.activity.channelId, include: includeFilter}))._response.parsedBody;
}

/**
* Signs the user out with the token server.
* @param context Context for the current turn of conversation with the user.
Expand Down
34 changes: 34 additions & 0 deletions libraries/botbuilder/tests/botFrameworkAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,15 @@ function assertResponse(res, statusCode, hasBody) {

describe(`BotFrameworkAdapter`, function () {
this.timeout(5000);

it(`should return the status of every connection the user has`, async function () {
const adapter = new AdapterUnderTest();
const context = new TurnContext(adapter, incomingMessage);
adapter.getTokenStatus(context)
.then((responses) => {
assert(responses.length > 0);
});
});

it(`should authenticateRequest() if no appId or appPassword.`, function (done) {
const req = new MockRequest(incomingMessage);
Expand Down Expand Up @@ -811,4 +820,29 @@ describe(`BotFrameworkAdapter`, function () {
}
assert(false, `should have thrown an error message`);
});

it(`should throw error if missing from in getTokenStatus()`, async function () {
try {
const adapter = new AdapterUnderTest();

await adapter.getTokenStatus({ activity: {} });
} catch (err) {
assert(err.message === 'BotFrameworkAdapter.getTokenStatus(): missing from or from.id',
`expected "BotFrameworkAdapter.getTokenStatus(): missing from or from.id" Error message, not "${ err.message }"`);
return;
}
assert(false, `should have thrown an error message`);
});

it(`should throw error if missing from.id in getTokenStatus()`, async function () {
try {
const adapter = new AdapterUnderTest();
await adapter.getTokenStatus({ activity: { from: {} } });
} catch (err) {
assert(err.message === 'BotFrameworkAdapter.getTokenStatus(): missing from or from.id',
`expected "BotFrameworkAdapter.getTokenStatus(): missing from or from.id" Error message, not "${ err.message }"`);
return;
}
assert(false, `should have thrown an error message`);
});
});
1 change: 1 addition & 0 deletions libraries/botframework-connector/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './auth';
export { ConnectorClient } from './connectorApi/connectorClient';
export { TokenApiClient, TokenApiModels } from './tokenApi/tokenApiClient';
export { EmulatorApiClient } from './emulatorApiClient';
export * from './tokenApi/models'