-
-
Notifications
You must be signed in to change notification settings - Fork 446
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add PATCH/GET /saml-applications/:id APIs
- Loading branch information
Showing
10 changed files
with
236 additions
and
66 deletions.
There are no files selected for viewing
100 changes: 100 additions & 0 deletions
100
packages/core/src/saml-applications/libraries/saml-applications.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,100 @@ | ||
import { | ||
ApplicationType, | ||
type SamlApplicationResponse, | ||
type PatchSamlApplication, | ||
} from '@logto/schemas'; | ||
import { generateStandardId } from '@logto/shared'; | ||
import { removeUndefinedKeys } from '@silverhand/essentials'; | ||
|
||
import type Queries from '#src/tenants/Queries.js'; | ||
import assertThat from '#src/utils/assert-that.js'; | ||
|
||
import { ensembleSamlApplication, generateKeyPairAndCertificate } from './utils.js'; | ||
|
||
export const createSamlApplicationsLibrary = (queries: Queries) => { | ||
const { | ||
applications: { findApplicationById, updateApplicationById }, | ||
samlApplicationSecrets: { | ||
insertSamlApplicationSecret, | ||
findSamlApplicationSecretsByApplicationId, | ||
}, | ||
samlApplicationConfigs: { | ||
findSamlApplicationConfigByApplicationId, | ||
updateSamlApplicationConfig, | ||
}, | ||
} = queries; | ||
|
||
const createNewSamlApplicationSecretForApplication = async ( | ||
applicationId: string, | ||
// Set certificate life span to 1 year by default. | ||
lifeSpanInDays = 365 | ||
) => { | ||
const { privateKey, certificate, notAfter } = await generateKeyPairAndCertificate( | ||
lifeSpanInDays | ||
); | ||
|
||
return insertSamlApplicationSecret({ | ||
id: generateStandardId(), | ||
applicationId, | ||
privateKey, | ||
certificate, | ||
expiresAt: Math.floor(notAfter.getTime() / 1000), | ||
active: false, | ||
}); | ||
}; | ||
|
||
const findSamlApplicationById = async ( | ||
applicationId: string | ||
): Promise<SamlApplicationResponse> => { | ||
const application = await findApplicationById(applicationId); | ||
assertThat(application.type === ApplicationType.SAML, 'application.saml.saml_application_only'); | ||
|
||
const [samlConfig, samlSecrets] = await Promise.all([ | ||
findSamlApplicationConfigByApplicationId(application.id), | ||
findSamlApplicationSecretsByApplicationId(application.id), | ||
]); | ||
|
||
return ensembleSamlApplication({ application, samlConfig, samlSecret: samlSecrets }); | ||
}; | ||
|
||
const updateSamlApplicationById = async ( | ||
id: string, | ||
patchApplicationObject: PatchSamlApplication | ||
): Promise<SamlApplicationResponse> => { | ||
const { name, description, customData, config } = patchApplicationObject; | ||
|
||
const [updatedApplication, upToDateSamlConfig, samlSecrets] = await Promise.all([ | ||
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing | ||
name || description || customData | ||
? updateApplicationById( | ||
id, | ||
removeUndefinedKeys({ | ||
name, | ||
description, | ||
customData, | ||
}) | ||
) | ||
: findApplicationById(id), | ||
config | ||
? updateSamlApplicationConfig({ | ||
set: config, | ||
where: { applicationId: id }, | ||
jsonbMode: 'replace', | ||
}) | ||
: findSamlApplicationConfigByApplicationId(id), | ||
findSamlApplicationSecretsByApplicationId(id), | ||
]); | ||
|
||
return ensembleSamlApplication({ | ||
application: updatedApplication, | ||
samlConfig: upToDateSamlConfig, | ||
samlSecret: samlSecrets, | ||
}); | ||
}; | ||
|
||
return { | ||
createNewSamlApplicationSecretForApplication, | ||
findSamlApplicationById, | ||
updateSamlApplicationById, | ||
}; | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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 was deleted.
Oops, something went wrong.
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
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.