-
Notifications
You must be signed in to change notification settings - Fork 442
feat: support ng-add for standalone applications #1800
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
Merged
FabianGosebrink
merged 6 commits into
damienbod:main
from
timdeschryver:standalone-ng-add
Jul 14, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc80a99
feat: support ng-add for standalone applications
timdeschryver cc7c5d3
export initial config
timdeschryver a8a6cce
fix: rename authWellknownEndpoint in migrations to authWellknownEndpo…
timdeschryver ed033bb
fix: add support for Angular 15
timdeschryver d18312f
fix: use correct name with http config
timdeschryver b9698f8
ci: add standalone smoketest
timdeschryver File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
78 changes: 78 additions & 0 deletions
78
projects/schematics/src/ng-add/actions/add-standalone-import.ts
This file contains hidden or 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,78 @@ | ||
import { Rule, SchematicContext, Tree } from '@angular-devkit/schematics'; | ||
import { | ||
addFunctionalProvidersToStandaloneBootstrap, | ||
callsProvidersFunction, | ||
} from '@schematics/angular/private/standalone'; | ||
import { insertImport } from '@schematics/angular/utility/ast-utils'; | ||
import { InsertChange } from '@schematics/angular/utility/change'; | ||
import * as ts from 'typescript'; | ||
import { getProject, readIntoSourceFile } from '../../utils/angular-utils'; | ||
import { NgAddOptions } from '../models/ng-add-options'; | ||
|
||
export function addStandaloneConfigsToProviders(options: NgAddOptions): Rule { | ||
return (host: Tree, context: SchematicContext) => { | ||
const project = getProject(host); | ||
|
||
const { fileName, configName } = options.standaloneInfo!; | ||
|
||
const standaloneConfigs = [ | ||
{ | ||
target: `${project.sourceRoot}/main.ts`, | ||
configName, | ||
configPath: `./auth/${fileName}`, | ||
}, | ||
]; | ||
|
||
standaloneConfigs.forEach(({ target, configName, configPath }) => { | ||
addProvider(host, context, configName, configPath, target); | ||
}); | ||
|
||
context.logger.info(`✅️ All imports done, please add the 'provideRouter()' as well if you don't have it provided yet.`); | ||
|
||
return host; | ||
}; | ||
} | ||
|
||
function addProvider(host: Tree, context: SchematicContext, configName: string, configPath: string, target: string) { | ||
const sourcefile = readIntoSourceFile(host, target); | ||
const providerFn = 'provideAuth'; | ||
if (callsProvidersFunction(host, sourcefile.fileName, providerFn)) { | ||
// exit because the store config is already provided | ||
return host; | ||
} | ||
|
||
const patchedConfigFile = addFunctionalProvidersToStandaloneBootstrap( | ||
host, | ||
sourcefile.fileName, | ||
providerFn, | ||
'angular-auth-oidc-client', | ||
[ts.factory.createIdentifier('authConfig')] | ||
); | ||
|
||
const configFileContent = host.read(patchedConfigFile); | ||
const source = ts.createSourceFile( | ||
patchedConfigFile, | ||
configFileContent?.toString('utf-8') || '', | ||
ts.ScriptTarget.Latest, | ||
true | ||
); | ||
|
||
|
||
const change = insertImport( | ||
source, | ||
patchedConfigFile, | ||
configName, | ||
configPath | ||
); | ||
|
||
const recorder = host.beginUpdate(patchedConfigFile); | ||
|
||
if (change instanceof InsertChange) { | ||
recorder.insertLeft(change.pos, change.toAdd); | ||
} | ||
|
||
host.commitUpdate(recorder); | ||
|
||
return host; | ||
|
||
} |
This file contains hidden or 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 |
---|---|---|
|
@@ -24,7 +24,7 @@ const IFRAME_SILENT_RENEW = `{ | |
|
||
const AZURE_AD_SILENT_RENEW = `{ | ||
authority: 'https://login.microsoftonline.com/<authorityUrlOrTenantId>/v2.0', | ||
authWellknownEndpoint: 'https://login.microsoftonline.com/common/v2.0', | ||
authWellknownEndpointUrl: 'https://login.microsoftonline.com/common/v2.0', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While testing the migration this gave a compile error, so I renamed this property to match the type. |
||
redirectUrl: window.location.origin, | ||
clientId: 'please-enter-clientId', | ||
scope: 'please-enter-scopes', // 'openid profile ' + your scopes | ||
|
@@ -41,7 +41,7 @@ const AZURE_AD_SILENT_RENEW = `{ | |
|
||
const AZURE_AD_REFRESH_TOKENS = `{ | ||
authority: 'https://login.microsoftonline.com/<authorityUrlOrTenantId>/v2.0', | ||
authWellknownEndpoint: 'https://login.microsoftonline.com/common/v2.0', | ||
authWellknownEndpointUrl: 'https://login.microsoftonline.com/common/v2.0', | ||
redirectUrl: window.location.origin, | ||
clientId: 'please-enter-clientId', | ||
scope: 'please-enter-scopes', // 'openid profile offline_access ' + your scopes | ||
|
@@ -93,5 +93,5 @@ const OIDC_PLAIN = `{ | |
renewTimeBeforeTokenExpiresInSeconds: 10, | ||
}`; | ||
|
||
export { DEFAULT_CONFIG, AZURE_AD_SILENT_RENEW, IFRAME_SILENT_RENEW, AZURE_AD_REFRESH_TOKENS, OIDC_PLAIN, AUTH_0, OAUTH_PAR }; | ||
export { AUTH_0, AZURE_AD_REFRESH_TOKENS, AZURE_AD_SILENT_RENEW, DEFAULT_CONFIG, IFRAME_SILENT_RENEW, OAUTH_PAR, OIDC_PLAIN }; | ||
|
This file contains hidden or 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
106 changes: 106 additions & 0 deletions
106
projects/schematics/src/ng-add/actions/copy-standalone-file.ts
This file contains hidden or 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,106 @@ | ||
import { normalize } from '@angular-devkit/core'; | ||
import { | ||
apply, | ||
chain, | ||
mergeWith, | ||
move, | ||
Rule, | ||
SchematicContext, | ||
SchematicsException, | ||
template, | ||
Tree, | ||
url | ||
} from '@angular-devkit/schematics'; | ||
import { getProject } from '../../utils/angular-utils'; | ||
import { NgAddOptions } from '../models/ng-add-options'; | ||
import { FlowType } from '../schema'; | ||
import { AUTH_0, AZURE_AD_REFRESH_TOKENS, AZURE_AD_SILENT_RENEW, DEFAULT_CONFIG, IFRAME_SILENT_RENEW, OAUTH_PAR, OIDC_PLAIN } from './configs'; | ||
|
||
export function copyStandaloneFile(options: NgAddOptions): Rule { | ||
return (host: Tree, context: SchematicContext) => { | ||
const project = getProject(host); | ||
|
||
const { fileName, filesFolder } = options.standaloneInfo!; | ||
|
||
const filePath = `${project.sourceRoot}/app/auth/${fileName}.ts`; | ||
if (host.exists(filePath)) { | ||
context.logger.info(`✅️ '${filePath}' already existing - skipping file create`); | ||
return host; | ||
} | ||
|
||
const templateConfig = getTemplateConfig(options); | ||
|
||
context.logger.info(`✅️ '${filePath}' will be created`); | ||
|
||
const templateSource = apply(url(`./files/${filesFolder}`), [ | ||
template(templateConfig), | ||
move(normalize(`${project.sourceRoot}/app/auth`)), | ||
]); | ||
|
||
return chain([mergeWith(templateSource)]); | ||
}; | ||
} | ||
|
||
function getTemplateConfig(options: NgAddOptions) { | ||
const { authorityUrlOrTenantId, flowType } = options; | ||
|
||
if (options.isHttpOption) { | ||
return { ts: 'ts', authorityUrlOrTenantId }; | ||
} | ||
|
||
const authConfig = getConfig(flowType, authorityUrlOrTenantId); | ||
|
||
return { ts: 'ts', authConfig }; | ||
} | ||
|
||
function getConfig(flowType: FlowType, authorityUrlOrTenantId: string) { | ||
let config = DEFAULT_CONFIG; | ||
|
||
switch (flowType) { | ||
case FlowType.OidcCodeFlowPkceAzureAdUsingIframeSilentRenew: { | ||
config = AZURE_AD_SILENT_RENEW; | ||
break; | ||
} | ||
|
||
case FlowType.OidcCodeFlowPkceAzureAdUsingRefreshTokens: { | ||
config = AZURE_AD_REFRESH_TOKENS; | ||
break; | ||
} | ||
|
||
case FlowType.OAuthPushAuthorizationRequestsUsingRefreshTokens: { | ||
config = OAUTH_PAR; | ||
break; | ||
} | ||
|
||
case FlowType.OidcCodeFlowPkceUsingIframeSilentRenew: { | ||
config = IFRAME_SILENT_RENEW; | ||
break; | ||
} | ||
|
||
case FlowType.OidcCodeFlowPkceUsingIframeSilentRenewGettingConfigFromHttp: { | ||
throw new SchematicsException(`With HTTP another module is used. No config but another module`); | ||
} | ||
|
||
case FlowType.OIDCCodeFlowPkce: { | ||
config = OIDC_PLAIN; | ||
break; | ||
} | ||
|
||
case FlowType.Auth0: { | ||
config = AUTH_0; | ||
break; | ||
} | ||
|
||
case FlowType.OidcCodeFlowPkceUsingRefreshTokens: | ||
case FlowType.DefaultConfig: { | ||
config = DEFAULT_CONFIG; | ||
break; | ||
} | ||
|
||
default: { | ||
throw new SchematicsException(`Could not parse flowType '${flowType}'`); | ||
} | ||
} | ||
|
||
return config.replace('<authorityUrlOrTenantId>', authorityUrlOrTenantId); | ||
} |
This file contains hidden or 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 hidden or 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
File renamed without changes.
5 changes: 5 additions & 0 deletions
5
projects/schematics/src/ng-add/files/auth-config-standalone/auth.config.__ts__
This file contains hidden or 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,5 @@ | ||
import { PassedInitialConfig } from 'angular-auth-oidc-client'; | ||
|
||
export const authConfig: PassedInitialConfig = { | ||
config: <%= authConfig %> | ||
} |
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Needed to be used to configure a standalone application in a type-safe way.