-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(type-b): update add new api logic to support oauth (#11515)
* perf(type-b): update add new api logic * perf: update message * perf: move common logic to configManager * perf: remove unexpected change * fix: reference issue --------- Co-authored-by: rentu <rentu@microsoft.com>
- Loading branch information
Showing
5 changed files
with
558 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
packages/fx-core/src/component/configManager/actionInjector.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
import { Utils } from "@microsoft/m365-spec-parser"; | ||
import fs from "fs-extra"; | ||
import { parseDocument } from "yaml"; | ||
import { InjectAPIKeyActionFailedError, InjectOAuthActionFailedError } from "../../error/common"; | ||
|
||
export class ActionInjector { | ||
static hasActionWithName(provisionNode: any, action: string, name: string): any { | ||
const hasAuthAction = provisionNode.items.some( | ||
(item: any) => item.get("uses") === action && item.get("with")?.get("name") === name | ||
); | ||
return hasAuthAction; | ||
} | ||
|
||
static getTeamsAppIdEnvName(provisionNode: any): string | undefined { | ||
for (const item of provisionNode.items) { | ||
if (item.get("uses") === "teamsApp/create") { | ||
return item.get("writeToEnvironmentFile")?.get("teamsAppId") as string; | ||
} | ||
} | ||
|
||
return undefined; | ||
} | ||
|
||
static generateAuthAction( | ||
actionName: string, | ||
authName: string, | ||
teamsAppIdEnvName: string, | ||
specRelativePath: string, | ||
envName: string, | ||
flow?: string | ||
): any { | ||
const result: any = { | ||
uses: actionName, | ||
with: { | ||
name: `${authName}`, | ||
appId: `\${{${teamsAppIdEnvName}}}`, | ||
apiSpecPath: specRelativePath, | ||
}, | ||
}; | ||
|
||
if (flow) { | ||
result.with.flow = flow; | ||
result.writeToEnvironmentFile = { | ||
configurationId: envName, | ||
}; | ||
} else { | ||
result.writeToEnvironmentFile = { | ||
registrationId: envName, | ||
}; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static async injectCreateOAuthAction( | ||
ymlPath: string, | ||
authName: string, | ||
specRelativePath: string | ||
): Promise<void> { | ||
const ymlContent = await fs.readFile(ymlPath, "utf-8"); | ||
const actionName = "oauth/register"; | ||
|
||
const document = parseDocument(ymlContent); | ||
const provisionNode = document.get("provision") as any; | ||
if (provisionNode) { | ||
const hasOAuthAction = ActionInjector.hasActionWithName(provisionNode, actionName, authName); | ||
if (!hasOAuthAction) { | ||
provisionNode.items = provisionNode.items.filter( | ||
(item: any) => item.get("uses") !== actionName && item.get("uses") !== "apiKey/register" | ||
); | ||
const teamsAppIdEnvName = ActionInjector.getTeamsAppIdEnvName(provisionNode); | ||
if (teamsAppIdEnvName) { | ||
const index: number = provisionNode.items.findIndex( | ||
(item: any) => item.get("uses") === "teamsApp/create" | ||
); | ||
const envName = Utils.getSafeRegistrationIdEnvName(`${authName}_CONFIGURATION_ID`); | ||
const flow = "authorizationCode"; | ||
const action = ActionInjector.generateAuthAction( | ||
actionName, | ||
authName, | ||
teamsAppIdEnvName, | ||
specRelativePath, | ||
envName, | ||
flow | ||
); | ||
provisionNode.items.splice(index + 1, 0, action); | ||
} else { | ||
throw new InjectOAuthActionFailedError(); | ||
} | ||
|
||
await fs.writeFile(ymlPath, document.toString(), "utf8"); | ||
} | ||
} else { | ||
throw new InjectOAuthActionFailedError(); | ||
} | ||
} | ||
|
||
static async injectCreateAPIKeyAction( | ||
ymlPath: string, | ||
authName: string, | ||
specRelativePath: string | ||
): Promise<void> { | ||
const ymlContent = await fs.readFile(ymlPath, "utf-8"); | ||
const actionName = "apiKey/register"; | ||
|
||
const document = parseDocument(ymlContent); | ||
const provisionNode = document.get("provision") as any; | ||
|
||
if (provisionNode) { | ||
const hasApiKeyAction = ActionInjector.hasActionWithName(provisionNode, actionName, authName); | ||
|
||
if (!hasApiKeyAction) { | ||
provisionNode.items = provisionNode.items.filter( | ||
(item: any) => item.get("uses") !== actionName && item.get("uses") !== "oauth/register" | ||
); | ||
const teamsAppIdEnvName = ActionInjector.getTeamsAppIdEnvName(provisionNode); | ||
if (teamsAppIdEnvName) { | ||
const index: number = provisionNode.items.findIndex( | ||
(item: any) => item.get("uses") === "teamsApp/create" | ||
); | ||
const envName = Utils.getSafeRegistrationIdEnvName(`${authName}_REGISTRATION_ID`); | ||
const action = ActionInjector.generateAuthAction( | ||
actionName, | ||
authName, | ||
teamsAppIdEnvName, | ||
specRelativePath, | ||
envName | ||
); | ||
provisionNode.items.splice(index + 1, 0, action); | ||
} else { | ||
throw new InjectAPIKeyActionFailedError(); | ||
} | ||
|
||
await fs.writeFile(ymlPath, document.toString(), "utf8"); | ||
} | ||
} else { | ||
throw new InjectAPIKeyActionFailedError(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.