Skip to content

Commit

Permalink
feat: add authentications
Browse files Browse the repository at this point in the history
  • Loading branch information
cfabianski committed Apr 12, 2020
1 parent 9a1598a commit 3611c75
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 67 deletions.
4 changes: 3 additions & 1 deletion config/knexfile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ module.exports = {
max: 10
},
migrations: {
tableName: 'knex_migrations'
tableName: 'knex_migrations',
directory: './migrations',
ext: 'ts'
}
}
}
15 changes: 15 additions & 0 deletions config/migrations/20200412215536_create_authentications.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as Knex from 'knex'

export async function up(knex: Knex): Promise<any> {
// tslint:disable-next-line:ter-prefer-arrow-callback
return knex.schema.createTable('authentications', function(t) {
t.bigIncrements('id')
t.string('buid')
t.string('auth_id')
t.json('user_attributes')
})
}

export async function down(knex: Knex): Promise<any> {
return knex.schema.dropTable('authentications')
}
7 changes: 1 addition & 6 deletions src/auth/v3/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ export default () => {
const router = Router()

router.use(session())
// TODO: remove
router.use('/test-session', (req: any, res, next) => {
const n = req.session.views || 0
req.session.views = Math.min(100, n + 1)
res.end(`${n} views`)
})

router.get('/callback', callbackContext, callbackConfig, callbackAuthId, authenticateAndRespond)

router.get(
Expand Down
21 changes: 9 additions & 12 deletions src/auth/v3/success.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { Response } from 'express'
import { AuthSuccessRequest } from './types'
import { asyncMiddleware } from '../../errorHandler'
import {
// updateAuthV3,
TAuthUserAttributes
} from '../../clients/integrations'
import { updateAuth, TAuthUserAttributes } from '../../clients/integrations'

export const authSuccess = asyncMiddleware(async (req: AuthSuccessRequest, res: Response) => {
const {
Expand Down Expand Up @@ -37,15 +34,15 @@ export const authSuccess = asyncMiddleware(async (req: AuthSuccessRequest, res:
userAttributes.callbackParamsJSON = JSON.stringify(req.query)
}

// const params = {
// clientId,
// buid,
// authId,
// userAttributes,
// servicesTableName: req.stageVariables.servicesTableId
// }
const params = {
buid,
authId,
userAttributes
}

console.log('[authSucces] userAttributes', params)

// await updateAuthV3(params)
await updateAuth(params)

res.header('Content-Type', 'text/html')
res.render('callback', { authId, error: '', error_description: '', integrationUuid: buid })
Expand Down
64 changes: 16 additions & 48 deletions src/clients/integrations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// import { SetupDetailsNotFound } from '../errors'
import { TIntegrationConfig, EAuthType } from '../auth/v3/types'
import '../../integrations'
import { dbClient } from '../db'

interface ICommonUserAttributes {
serviceName: string
Expand Down Expand Up @@ -34,59 +35,26 @@ export type TOAuth2UserAttributes = ICommonUserAttributes & {
// const nameUserId = ({ clientId, buid, authId }: { clientId: string; buid: string; authId: string }) =>
// [clientId, buid, authId].join(':')

// interface IAuthParams {
// servicesTableName: string
// clientId: string
// buid: string
// authId: string
// }
interface IAuthParams {
buid: string
authId: string
}

export type TAuthUserAttributes = TOAuth1UserAttributes | TOAuth2UserAttributes

// export const updateAuthV3 = async ({
// servicesTableName,
// clientId,
// buid,
// authId,
// userAttributes
// }: IAuthParams & { userAttributes: TAuthUserAttributes }) => {
// const { UpdateExpression, ExpressionAttributeValues } = buildExpression(userAttributes)
// const updateParams = {
// UpdateExpression,
// ExpressionAttributeValues,
// TableName: servicesTableName,
// Key: {
// nameUserId: nameUserId({ clientId, buid, authId })
// }
// }

// await dynamoDb.update(updateParams).promise()
// }

// export const revokeAuthV3 = async ({ servicesTableName, clientId, buid, authId }: IAuthParams) => {
// const revokeParams = {
// TableName: servicesTableName,
// Key: {
// nameUserId: nameUserId({ clientId, buid, authId })
// }
// }

// await dynamoDb.delete(revokeParams).promise()
// }

// tslint:disable-next-line:max-line-length
// export const getAuth = async <T = TAuthUserAttributes>({ servicesTableName, clientId, buid, authId }: IAuthParams) => {
// const getParams = {
// TableName: servicesTableName,
// Key: {
// nameUserId: nameUserId({ clientId, buid, authId })
// }
// }
export const updateAuth = async ({
buid,
authId,
userAttributes
}: IAuthParams & { userAttributes: TAuthUserAttributes }) => {
const client = dbClient()
await client('authentications').insert({ buid, auth_id: authId, user_attributes: userAttributes })

// const item = (await dynamoDb.get(getParams).promise()).Item
const res = await client('authentications').where({ buid, auth_id: authId })
console.log('[updateAuth] res', res)
}

// return item as T | undefined
// }
export const getAuth = async <T = TAuthUserAttributes>({ buid, authId }: IAuthParams) => {}

export const getConfig = async ({ buid }: { buid: string }) => {
let item = {} as any
Expand Down

0 comments on commit 3611c75

Please sign in to comment.