Skip to content

Commit

Permalink
Merge pull request #47 from centre-for-effective-altruism/dev
Browse files Browse the repository at this point in the history
Deploy upgrading node-auth0 from v2 to v4
  • Loading branch information
Will-Howard authored Jul 31, 2024
2 parents 1702893 + e4948dc commit 20d7959
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 622 deletions.
2 changes: 1 addition & 1 deletion cli/commands/db/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default async function run() {
...ourCustomScripts,
}
options.customScripts = updatedCustomScripts
await auth0.updateConnection({ id: dbConnection.id }, { options })
await auth0.connections.update({ id: dbConnection.id }, { options })
console.log(`Scripts updated ${green(`\u2713`)}`)
} catch (err) {
console.error(red((err as Error).message))
Expand Down
14 changes: 8 additions & 6 deletions cli/commands/login/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async function deployLoginTemplate() {
const newTemplate = (
await fs.readFile(path.join(__dirname, '../../../templates/login.liquid'))
).toString()
await branding.setUniversalLoginTemplate(undefined, {
await branding.setUniversalLoginTemplate({
template: newTemplate,
})
}
Expand All @@ -31,9 +31,11 @@ async function deployCustomText() {
).toString()
)

auth0.prompts.updateCustomTextByLanguage({
prompt: 'signup',
language: 'en',
body: customText,
})
auth0.prompts.updateCustomTextByLanguage(
{
prompt: 'signup',
language: 'en',
},
customText
)
}
18 changes: 13 additions & 5 deletions cli/commands/login/diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ async function diffLoginTemplate() {
const branding = auth0.branding
let existingTemplate
try {
existingTemplate = (await branding.getUniversalLoginTemplate()).body
const response = await branding.getUniversalLoginTemplate()
if (typeof response.data === 'string') {
throw new Error(
'Expected an object for the template, but received a string.'
)
}
existingTemplate = response.data.body
} catch (err) {
if ((err as { statusCode?: number }).statusCode === 404) {
existingTemplate = ''
Expand All @@ -37,10 +43,12 @@ async function diffLoginTemplate() {
}

async function diffCustomText() {
const existingCustomText = await auth0.prompts.getCustomTextByLanguage({
prompt: 'signup',
language: 'en',
})
const existingCustomText = (
await auth0.prompts.getCustomTextByLanguage({
prompt: 'signup',
language: 'en',
})
).data
const sortedExistingCustomText = deepSortObject(existingCustomText)

const newCustomText = JSON.parse(
Expand Down
4 changes: 2 additions & 2 deletions cli/commands/rules/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ export default async function run() {
if (existingRule?.id) {
// Update an existing rule
formatUpdateRuleMessage(ruleDef.name, true)
await auth0.updateRule(
await auth0.rules.update(
{ id: existingRule.id },
{ enabled: ruleDef.enabled, script, order }
)
console.log(`Rule updated ${green(`\u2713`)}`)
} else {
// Create a new rule
formatUpdateRuleMessage(ruleDef.name, false)
await auth0.createRule({
await auth0.rules.create({
name: ruleDef.name,
enabled: ruleDef.enabled,
script,
Expand Down
31 changes: 22 additions & 9 deletions cli/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Client, Connection, Role, Rule } from 'auth0'
import {
ApiResponse,
Client,
Connection,
GetOrganizationMemberRoles200ResponseOneOfInner as Role,
Rule,
} from 'auth0'
import { fs } from 'mz'
import path from 'path'
import auth0 from './client'
Expand All @@ -16,30 +22,37 @@ export function paginateQuery<T>(
}: {
page: number
per_page: number
}) => Promise<T[]>
}) => Promise<ApiResponse<T[]>>
): (page?: number) => Promise<T[]> {
const fn = async (page = 0): Promise<T[]> => {
const res = await method({ page, per_page: RESULTS_PER_PAGE })
if (res.length < RESULTS_PER_PAGE) {
return res
const items = res.data
if (items.length < RESULTS_PER_PAGE) {
return items
}
return [...res, ...(await fn(page + 1))]
return [...items, ...(await fn(page + 1))]
}
return fn
}

/** Recursively page through all rules on the Auth0 tenant */
export const getAllRules = paginateQuery<Rule>(auth0.getRules.bind(auth0))
export const getAllRules = paginateQuery<Rule>(
auth0.rules.getAll.bind(auth0.rules)
)

/** Recursively page through all clients (applications) on the Auth0 tenant */
export const getAllClients = paginateQuery<Client>(auth0.getClients.bind(auth0))
export const getAllClients = paginateQuery<Client>(
auth0.clients.getAll.bind(auth0.clients)
)

/** Recursively page through all roles on the Auth0 tenant */
export const getAllRoles = paginateQuery<Role>(auth0.getRoles.bind(auth0))
export const getAllRoles = paginateQuery<Role>(
auth0.roles.getAll.bind(auth0.roles)
)

/** Recursively page through all connections on the Auth0 tenant */
export const getAllConnections = paginateQuery<Connection>(
auth0.getConnections.bind(auth0)
auth0.connections.getAll.bind(auth0.connections)
)

const AUTOGENERATED_COMMENT = `
Expand Down
31 changes: 0 additions & 31 deletions cli/types/login.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
]
},
"dependencies": {
"auth0": "2.42.0",
"auth0": "^4.7.0",
"chalk": "^4.1.0",
"diff": "^5.0.0",
"dotenv": "^8.2.0",
Expand Down
27 changes: 20 additions & 7 deletions scripts/db/types/db-types.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
import { User } from 'auth0'

/** Shape of user object that Database Action Scripts expect to be returned */
interface CallbackUser extends User {
/**
* Shape of user object that Database Action Scripts expect to be returned.
*
* There is no predefined type for this, we are using this documentation as
* a guide for what is allowed:
* https://auth0.com/docs/manage-users/user-accounts/user-profiles/normalized-user-profile-schema
*/
export interface CallbackUser {
/**
* This appears to be necessary, despite no documentation I could find in a
* cursory inspection
*/
id: string
email: string
email_verified?: boolean
nickname?: string
given_name?: string
family_name?: string
picture?: string
}
/** Signature of Database Action Script callback */
type DbScriptCallback = (error: Error | null, person?: CallbackUser) => any
export type DbScriptCallback = (
error: Error | null,
person?: CallbackUser
) => any

/** Configuration available from connection environment variables */
interface DbConfiguration {
export interface DbConfiguration {
POSTGRES_USERNAME: string
POSTGRES_PASSWORD: string
POSTGRES_HOST: string
Expand All @@ -23,7 +36,7 @@ interface DbConfiguration {
}

/** Forum user */
type ForumUser = {
export type ForumUser = {
_id: string
displayName: string
services: {
Expand Down
Loading

0 comments on commit 20d7959

Please sign in to comment.