Skip to content

Commit

Permalink
[#17482372] Add token name to GetUser from Azure ADB2C (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
AleDore authored Sep 18, 2020
1 parent 419bc30 commit 618aff4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
30 changes: 21 additions & 9 deletions GetUser/__tests__/handler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
import { GraphRbacManagementClient } from "@azure/graph";
import { isRight, left, right } from "fp-ts/lib/Either";
import { fromEither, fromLeft } from "fp-ts/lib/TaskEither";
import { NonEmptyString } from "italia-ts-commons/lib/strings";
import { UserInfo } from "../../generated/definitions/UserInfo";
import * as ApimUtils from "../../utils/apim";
import { IAzureApimConfig, IServicePrincipalCreds } from "../../utils/apim";
Expand Down Expand Up @@ -83,6 +84,8 @@ spyOnGetAdb2cClient.mockImplementation(() =>
const mockLog = jest.fn();
const mockedContext = { log: { error: mockLog } };

const fakeAdb2cExtensionAppClientId = "extension-client-id" as NonEmptyString;

// tslint:disable-next-line:no-big-function
describe("GetUser", () => {
it("should return an internal error response if the API management client can not be got", async () => {
Expand All @@ -93,7 +96,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand All @@ -112,7 +116,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand All @@ -129,7 +134,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand All @@ -148,7 +154,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand All @@ -171,7 +178,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand All @@ -194,7 +202,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand All @@ -217,7 +226,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand All @@ -240,7 +250,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand Down Expand Up @@ -340,7 +351,8 @@ describe("GetUser", () => {
const getUserHandler = GetUserHandler(
fakeAdb2cCreds,
fakeServicePrincipalCredentials,
fakeApimConfig
fakeApimConfig,
fakeAdb2cExtensionAppClientId
);

const response = await getUserHandler(
Expand Down
13 changes: 7 additions & 6 deletions GetUser/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ function getUserSubscriptions(
export function GetUserHandler(
adb2cCredentials: IServicePrincipalCreds,
servicePrincipalCreds: IServicePrincipalCreds,
azureApimConfig: IAzureApimConfig
azureApimConfig: IAzureApimConfig,
adb2cExtensionAppClientId: NonEmptyString
): IGetSubscriptionKeysHandler {
return async (context, _, email) => {
const internalErrorHandler = (errorMessage: string, error: Error) =>
Expand Down Expand Up @@ -217,9 +218,7 @@ export function GetUserHandler(
return {
...taskResults,
token_name:
adb2User[
`adb2User.extension_${adb2cCredentials.clientId}_token_name`
]
adb2User[`extension_${adb2cExtensionAppClientId}_token_name`]
};
})
)
Expand Down Expand Up @@ -270,12 +269,14 @@ export function GetUserHandler(
export function GetUser(
adb2cCredentials: IServicePrincipalCreds,
servicePrincipalCreds: IServicePrincipalCreds,
azureApimConfig: IAzureApimConfig
azureApimConfig: IAzureApimConfig,
adb2cExtensionAppClientId: NonEmptyString
): express.RequestHandler {
const handler = GetUserHandler(
adb2cCredentials,
servicePrincipalCreds,
azureApimConfig
azureApimConfig,
adb2cExtensionAppClientId
);

const middlewaresWrap = withRequestMiddlewares(
Expand Down
11 changes: 10 additions & 1 deletion GetUser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const azureApimConfig = {
subscriptionId: getRequiredStringEnv("AZURE_SUBSCRIPTION_ID")
};

const adb2cExtensionAppClientId = getRequiredStringEnv(
"ADB2C_EXTENSION_APP_CLIENT_ID"
);

// tslint:disable-next-line: no-let
let logger: Context["log"] | undefined;
const contextTransport = new AzureContextTransport(() => logger, {
Expand All @@ -42,7 +46,12 @@ secureExpressApp(app);
// Add express route
app.get(
"/adm/users/:email",
GetUser(adb2cCreds, servicePrincipalCreds, azureApimConfig)
GetUser(
adb2cCreds,
servicePrincipalCreds,
azureApimConfig,
adb2cExtensionAppClientId
)
);

const azureFunctionHandler = createAzureFunctionHandler(app);
Expand Down
1 change: 1 addition & 0 deletions env.example
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ COSMOSDB_NAME=<COSMOSDB_NAME>
ADB2C_CLIENT_ID=1
ADB2C_CLIENT_KEY=1
ADB2C_TENANT_ID=1
ADB2C_EXTENSION_APP_CLIENT_ID=1
SERVICE_PRINCIPAL_CLIENT_ID=1
SERVICE_PRINCIPAL_SECRET=1
SERVICE_PRINCIPAL_TENANT_ID=1
Expand Down

0 comments on commit 618aff4

Please sign in to comment.