Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/commands/auth/connections/body.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import test from 'ava'
import { clean_secret, connection_create_body, public_connection } from './body'

test('passwordless_email: name, type and on by default — nothing else', t => {
t.deepEqual(
connection_create_body({ name: 'email', type: 'passwordless_email' }),
{
connection_name: 'email',
connection_type: 'passwordless_email',
enabled: true
}
)
})

test('--no-enabled creates it off', t => {
t.is(
connection_create_body({ name: 'db', type: 'database', enabled: false })
.enabled,
false
)
})

test('a social provider sends only the provider fields given', t => {
t.deepEqual(
connection_create_body({
name: 'okta',
type: 'oidc',
clientId: 'abc',
clientSecret: 's3cr3t',
issuer: 'https://acme.okta.com',
scope: ['openid', 'email'],
disableSignup: true
}),
{
connection_name: 'okta',
connection_type: 'oidc',
enabled: true,
client_id: 'abc',
client_secret: 's3cr3t',
issuer: 'https://acme.okta.com',
scope: ['openid', 'email'],
disable_signup: true
}
)
// Google without a client id: Faable's shared app, no credentials sent.
t.false(
'client_id' in
connection_create_body({ name: 'google', type: 'google_oauth2' })
)
})

test('email codes and passwords refuse provider credentials', t => {
const err = t.throws(() =>
connection_create_body({
name: 'email',
type: 'passwordless_email',
clientId: 'abc'
})
)
t.regex(err!.message, /--client-id/)
t.throws(() =>
connection_create_body({ name: 'db', type: 'database', scope: ['x'] })
)
})

test('the client secret never comes back, not even in JSON', t => {
t.deepEqual(
public_connection({
id: 'connection_1',
client_secret: 'k',
client_id: 'c'
}),
{ id: 'connection_1', client_secret: '[hidden]', client_id: 'c' }
)
t.deepEqual(public_connection({ id: 'connection_1' }), { id: 'connection_1' })
})

test('a secret from a file or stdin loses its trailing newline, a PEM keeps its lines', t => {
t.is(clean_secret('s3cr3t\n', 'stdin'), 's3cr3t')
const pem = '-----BEGIN PRIVATE KEY-----\nAAA\n-----END PRIVATE KEY-----'
t.is(clean_secret(pem + '\n', 'key.p8'), pem)
t.throws(() => clean_secret(' \n', 'stdin'), { message: /empty/ })
})

test('apple: the team and key ids go in provider_options, and only for apple', t => {
t.deepEqual(
connection_create_body({
name: 'apple',
type: 'apple',
clientId: 'com.example.web',
clientSecret: 'PEM',
appleTeamId: 'ABCDE12345',
appleKeyId: 'KEY123'
}).provider_options,
{ team_id: 'ABCDE12345', key_id: 'KEY123' }
)
t.throws(
() =>
connection_create_body({
name: 'google',
type: 'google_oauth2',
appleTeamId: 'ABCDE12345'
}),
{ message: /only apply to apple/ }
)
})
129 changes: 129 additions & 0 deletions src/commands/auth/connections/body.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { CliError } from '../../../lib/errors'

// The connection types the Auth API accepts (auth: schemas/ConnectionType.ts).
// The strings are permanent there: persisted on every connection and published
// in the OpenAPI enum.
export const CONNECTION_TYPES = [
'passwordless_email',
'database',
'google_oauth2',
'github',
'microsoft',
'apple',
'facebook',
'linkedin',
'discord',
'gitlab',
'slack',
'x',
'twitch',
'bitbucket',
'spotify',
'figma',
'oidc',
'custom'
] as const
export type ConnectionTypeName = (typeof CONNECTION_TYPES)[number]

// Email codes and username/password keep their users in the tenant: there is
// no provider to hand a client id or a secret to.
const LOCAL_TYPES: ConnectionTypeName[] = ['passwordless_email', 'database']

export interface ConnectionCreateInput {
name: string
type: ConnectionTypeName
enabled?: boolean
clientId?: string
clientSecret?: string
issuer?: string
authorizeUrl?: string
tokenUrl?: string
userinfoUrl?: string
jwksUrl?: string
scope?: string[]
organization?: string
disableSignup?: boolean
appleTeamId?: string
appleKeyId?: string
}

const PROVIDER_FIELDS: [keyof ConnectionCreateInput, string, string][] = [
['clientId', '--client-id', 'client_id'],
['clientSecret', '--client-secret-file', 'client_secret'],
['issuer', '--issuer', 'issuer'],
['authorizeUrl', '--authorize-url', 'authorize_url'],
['tokenUrl', '--token-url', 'token_url'],
['userinfoUrl', '--userinfo-url', 'userinfo_url'],
['jwksUrl', '--jwks-url', 'jwks_url'],
['organization', '--organization', 'organization']
]

/**
* The POST /connection body. Only what was given travels: a social provider
* left without --client-id keeps Faable's shared app where there is one, and
* the server decides what a type requires (it is the source of truth).
*/
export const connection_create_body = (
input: ConnectionCreateInput
): Record<string, unknown> => {
if (LOCAL_TYPES.includes(input.type)) {
const given = PROVIDER_FIELDS.filter(([k]) => input[k] !== undefined)
if (given.length || input.scope?.length) {
const flags = [
...given.map(([, flag]) => flag),
...(input.scope?.length ? ['--scope'] : [])
]
throw new CliError(
'usage',
`${input.type} has no identity provider: ${flags.join(', ')} only apply to social and OIDC connections.`
)
}
}
const body: Record<string, unknown> = {
connection_name: input.name,
connection_type: input.type,
enabled: input.enabled ?? true
}
for (const [k, , field] of PROVIDER_FIELDS) {
if (input[k] !== undefined && input[k] !== '') body[field] = input[k]
}
if (input.scope?.length) body.scope = input.scope
// Apple's client secret is a JWT Faable signs with the .p8 key; the
// signature needs these two identifiers.
if (input.appleTeamId || input.appleKeyId) {
if (input.type !== 'apple') {
throw new CliError(
'usage',
'--apple-team-id and --apple-key-id only apply to apple connections.'
)
}
body.provider_options = {
...(input.appleTeamId ? { team_id: input.appleTeamId } : {}),
...(input.appleKeyId ? { key_id: input.appleKeyId } : {})
}
}
if (input.disableSignup !== undefined)
body.disable_signup = input.disableSignup
return body
}

/**
* What the CLI shows of a connection: never its client secret (Apple's is a
* private key), not even with --json, where it would end up in a script's log.
*/
export const public_connection = (c: any) => {
if (!c || typeof c !== 'object') return c
const { client_secret, ...rest } = c
return client_secret ? { ...rest, client_secret: '[hidden]' } : rest
}

// A secret from a file, or from stdin with `-`: never from argv, where `ps`
// and the shell history read it. Surrounding whitespace goes (a trailing
// newline from `echo` or an editor is never part of a secret); a PEM key keeps
// its inner lines.
export const clean_secret = (raw: string, source: string): string => {
const secret = raw.trim()
if (!secret)
throw new CliError('usage', `The client secret from ${source} is empty.`)
return secret
}
Loading
Loading