-
-
Notifications
You must be signed in to change notification settings - Fork 452
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
feat(core): add PATCH/GET /saml-applications/:id
APIs
#6827
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { | ||
ApplicationType, | ||
type SamlApplicationResponse, | ||
type PatchSamlApplication, | ||
} from '@logto/schemas'; | ||
import { generateStandardId } from '@logto/shared'; | ||
import { removeUndefinedKeys } from '@silverhand/essentials'; | ||
|
||
import RequestError from '#src/errors/RequestError/index.js'; | ||
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 }, | ||
samlApplicationConfigs: { | ||
findSamlApplicationConfigByApplicationId, | ||
updateSamlApplicationConfig, | ||
}, | ||
} = queries; | ||
|
||
const createSamlApplicationSecret = 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 (id: string): Promise<SamlApplicationResponse> => { | ||
const application = await findApplicationById(id); | ||
assertThat( | ||
application.type === ApplicationType.SAML, | ||
new RequestError({ | ||
code: 'application.saml.saml_application_only', | ||
status: 422, | ||
}) | ||
); | ||
|
||
const samlConfig = await findSamlApplicationConfigByApplicationId(application.id); | ||
|
||
return ensembleSamlApplication({ application, samlConfig }); | ||
}; | ||
|
||
const updateSamlApplicationById = async ( | ||
id: string, | ||
patchApplicationObject: PatchSamlApplication | ||
): Promise<SamlApplicationResponse> => { | ||
const { config, ...applicationData } = patchApplicationObject; | ||
const originalApplication = await findApplicationById(id); | ||
|
||
assertThat( | ||
originalApplication.type === ApplicationType.SAML, | ||
new RequestError({ | ||
code: 'application.saml.saml_application_only', | ||
status: 422, | ||
}) | ||
); | ||
|
||
const [updatedApplication, upToDateSamlConfig] = await Promise.all([ | ||
Object.keys(applicationData).length > 0 | ||
? updateApplicationById(id, removeUndefinedKeys(applicationData)) | ||
: originalApplication, | ||
config | ||
? updateSamlApplicationConfig({ | ||
set: config, | ||
where: { applicationId: id }, | ||
jsonbMode: 'replace', | ||
}) | ||
: findSamlApplicationConfigByApplicationId(id), | ||
]); | ||
|
||
return ensembleSamlApplication({ | ||
application: updatedApplication, | ||
samlConfig: upToDateSamlConfig, | ||
}); | ||
}; | ||
|
||
return { | ||
createSamlApplicationSecret, | ||
findSamlApplicationById, | ||
updateSamlApplicationById, | ||
}; | ||
}; |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,18 @@ | ||
import crypto from 'node:crypto'; | ||
|
||
import { | ||
type SamlApplicationResponse, | ||
type Application, | ||
type SamlApplicationConfig, | ||
type SamlAcsUrl, | ||
BindingType, | ||
} from '@logto/schemas'; | ||
import { addDays } from 'date-fns'; | ||
import forge from 'node-forge'; | ||
|
||
import RequestError from '#src/errors/RequestError/index.js'; | ||
import assertThat from '#src/utils/assert-that.js'; | ||
|
||
export const generateKeyPairAndCertificate = async (lifeSpanInDays = 365) => { | ||
const keypair = forge.pki.rsa.generateKeyPair({ bits: 4096 }); | ||
return createCertificate(keypair, lifeSpanInDays); | ||
|
@@ -23,7 +33,7 @@ | |
cert.validity.notAfter = notAfter; | ||
/* eslint-enable @silverhand/fp/no-mutation */ | ||
|
||
// TODO: read from tenant config or let user customize before downloading | ||
Check warning on line 36 in packages/core/src/saml-applications/libraries/utils.ts GitHub Actions / ESLint Report Analysispackages/core/src/saml-applications/libraries/utils.ts#L36
|
||
const subjectAttributes: forge.pki.CertificateField[] = [ | ||
{ | ||
name: 'commonName', | ||
|
@@ -56,3 +66,36 @@ | |
notAfter, | ||
}; | ||
}; | ||
|
||
/** | ||
* According to the design, a SAML app will be associated with multiple records from various tables. | ||
* Therefore, when complete SAML app data is required, it is necessary to retrieve multiple related records and assemble them into a comprehensive SAML app dataset. This dataset includes: | ||
* - A record from the `applications` table with a `type` of `SAML` | ||
* - A record from the `saml_application_configs` table | ||
*/ | ||
export const ensembleSamlApplication = ({ | ||
application, | ||
samlConfig, | ||
}: { | ||
application: Application; | ||
samlConfig: Pick<SamlApplicationConfig, 'attributeMapping' | 'entityId' | 'acsUrl'>; | ||
}): SamlApplicationResponse => { | ||
return { | ||
...application, | ||
...samlConfig, | ||
}; | ||
}; | ||
|
||
/** | ||
* Only HTTP-POST binding is supported for receiving SAML assertions at the moment. | ||
*/ | ||
export const validateAcsUrl = (acsUrl: SamlAcsUrl) => { | ||
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. Do we need to validate the URL format here? 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. Good call, updated |
||
const { binding } = acsUrl; | ||
assertThat( | ||
binding === BindingType.POST, | ||
new RequestError({ | ||
code: 'application.saml.acs_url_binding_not_supported', | ||
status: 422, | ||
}) | ||
); | ||
}; | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
const updateSamlApplicationConfig = buildUpdateWhereWithPool(pool)(SamlApplicationConfigs, true); | ||
|
||
const findSamlApplicationConfigByApplicationId = async (applicationId: string) => | ||
pool.maybeOne<SamlApplicationConfig>(sql` | ||
pool.one<SamlApplicationConfig>(sql` | ||
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. This config should be nullable? 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. When creating a SAML app, a record in the 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. According to the design of the console, when creating a SAML app, you only need to provide |
||
select ${sql.join(Object.values(fields), sql`, `)} | ||
from ${table} | ||
where ${fields.applicationId}=${applicationId} | ||
|
This file was deleted.
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.
ditto: status: 422