-
-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6069 from logto-io/gao-org-jit-sso-impl
feat(core): organization jit sso
- Loading branch information
Showing
21 changed files
with
394 additions
and
250 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import type { BindMfa, MfaVerification } from '@logto/schemas'; | ||
import { MfaFactor, UsersPasswordEncryptionMethod } from '@logto/schemas'; | ||
import { generateStandardId } from '@logto/shared'; | ||
|
||
import { encryptPassword } from '#src/utils/password.js'; | ||
|
||
export const encryptUserPassword = async ( | ||
password: string | ||
): Promise<{ | ||
passwordEncrypted: string; | ||
passwordEncryptionMethod: UsersPasswordEncryptionMethod; | ||
}> => { | ||
const passwordEncryptionMethod = UsersPasswordEncryptionMethod.Argon2i; | ||
const passwordEncrypted = await encryptPassword(password, passwordEncryptionMethod); | ||
|
||
return { passwordEncrypted, passwordEncryptionMethod }; | ||
}; | ||
|
||
/** | ||
* Convert bindMfa to mfaVerification, add common fields like "id" and "createdAt" | ||
* and transpile formats like "codes" to "code" for backup code | ||
*/ | ||
export const convertBindMfaToMfaVerification = (bindMfa: BindMfa): MfaVerification => { | ||
const { type } = bindMfa; | ||
const base = { | ||
id: generateStandardId(), | ||
createdAt: new Date().toISOString(), | ||
}; | ||
|
||
if (type === MfaFactor.BackupCode) { | ||
const { codes } = bindMfa; | ||
|
||
return { | ||
...base, | ||
type, | ||
codes: codes.map((code) => ({ code })), | ||
}; | ||
} | ||
|
||
if (type === MfaFactor.TOTP) { | ||
const { secret } = bindMfa; | ||
|
||
return { | ||
...base, | ||
type, | ||
key: secret, | ||
}; | ||
} | ||
|
||
const { credentialId, counter, publicKey, transports, agent } = bindMfa; | ||
return { | ||
...base, | ||
type, | ||
credentialId, | ||
counter, | ||
publicKey, | ||
transports, | ||
agent, | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { | ||
OrganizationJitRoles, | ||
OrganizationJitSsoConnectors, | ||
Organizations, | ||
SsoConnectors, | ||
} from '@logto/schemas'; | ||
import { type CommonQueryMethods, sql } from '@silverhand/slonik'; | ||
|
||
import { TwoRelationsQueries } from '#src/utils/RelationQueries.js'; | ||
import { convertToIdentifiers } from '#src/utils/sql.js'; | ||
|
||
import { type JitOrganization } from './email-domains.js'; | ||
|
||
const { table, fields } = convertToIdentifiers(OrganizationJitSsoConnectors); | ||
|
||
export class SsoConnectorQueries extends TwoRelationsQueries< | ||
typeof Organizations, | ||
typeof SsoConnectors | ||
> { | ||
constructor(pool: CommonQueryMethods) { | ||
super(pool, OrganizationJitSsoConnectors.table, Organizations, SsoConnectors); | ||
} | ||
|
||
async getJitOrganizations(ssoConnectorId?: string): Promise<readonly JitOrganization[]> { | ||
if (!ssoConnectorId) { | ||
return []; | ||
} | ||
|
||
const { fields } = convertToIdentifiers(OrganizationJitSsoConnectors, true); | ||
const organizationJitRoles = convertToIdentifiers(OrganizationJitRoles, true); | ||
return this.pool.any<JitOrganization>(sql` | ||
select | ||
${fields.organizationId}, | ||
array_remove( | ||
array_agg(${organizationJitRoles.fields.organizationRoleId}), | ||
null | ||
) as "organizationRoleIds" | ||
from ${table} | ||
left join ${organizationJitRoles.table} | ||
on ${fields.organizationId} = ${organizationJitRoles.fields.organizationId} | ||
where ${fields.ssoConnectorId} = ${ssoConnectorId} | ||
group by ${fields.organizationId} | ||
`); | ||
} | ||
} |
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.