Merge pull request #11 from mark-tesobe/develop

FIX: Login user
This commit is contained in:
Simon Redfern 2023-06-05 22:32:54 +02:00 committed by GitHub
commit 1bee07983b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 29 deletions

View File

@ -1,4 +1,4 @@
import { Controller, Req, Res, Get, Delete, Post, Put } from 'routing-controllers'
import { Controller, Session, Req, Res, Get, Delete, Post, Put } from 'routing-controllers'
import { Request, Response } from 'express'
import OBPClientService from '../services/OBPClientService'
import { Service } from 'typedi'
@ -8,28 +8,44 @@ import { Service } from 'typedi'
export class OBPController {
constructor(private obpClientService: OBPClientService) {}
@Get('/get')
async get(@Req() request: Request, @Res() response: Response): Response {
async get(@Session() session: any, @Req() request: Request, @Res() response: Response): Response {
const path = request.query.path
return response.json(await this.obpClientService.get(path))
const oauthConfig = session['clientConfig']
return response.json(await this.obpClientService.get(path, oauthConfig))
}
@Post('/create')
async create(@Req() request: Request, @Res() response: Response): Response {
async create(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
const path = request.query.path
const data = request.body
return response.json(await this.obpClientService.create(path, data))
const oauthConfig = session['clientConfig']
return response.json(await this.obpClientService.create(path, data, oauthConfig))
}
@Put('/update')
async update(@Req() request: Request, @Res() response: Response): Response {
async update(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
const path = request.query.path
const data = request.body
return response.json(await this.obpClientService.update(path, data))
const oauthConfig = session['clientConfig']
return response.json(await this.obpClientService.update(path, data, oauthConfig))
}
@Delete('/delete')
async delete(@Req() request: Request, @Res() response: Response): Response {
async delete(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
const path = request.query.path
return response.json(await this.obpClientService.discard(path))
const oauthConfig = session['clientConfig']
return response.json(await this.obpClientService.discard(path, oauthConfig))
}
}

View File

@ -1,4 +1,4 @@
import { Controller, Req, Res, Get } from 'routing-controllers'
import { Controller, Session, Req, Res, Get } from 'routing-controllers'
import { Request, Response } from 'express'
import OBPClientService from '../services/OBPClientService'
import OauthInjectedService from '../services/OauthInjectedService'
@ -14,17 +14,28 @@ export class UserController {
private oauthInjectedService: OauthInjectedService
) {}
@Get('/logoff')
async logout(@Req() request: Request, @Res() response: Response): Response {
async logout(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
this.oauthInjectedService.requestTokenKey = undefined
this.oauthInjectedService.requestTokenSecret = undefined
this.obpClientService.setAccessToken(undefined, undefined)
session['clientConfig'] = undefined
response.redirect(this.obpExplorerHome)
return response
}
@Get('/current')
async current(@Req() request: Request, @Res() response: Response): Response {
async current(
@Session() session: any,
@Req() request: Request,
@Res() response: Response
): Response {
const oauthConfig = session['clientConfig']
const version = this.obpClientService.getOBPVersion()
return response.json(await this.obpClientService.get(`/obp/${version}/users/current`))
return response.json(
await this.obpClientService.get(`/obp/${version}/users/current`, oauthConfig)
)
}
}

View File

@ -15,6 +15,7 @@ export default class OauthAccessTokenMiddleware implements ExpressMiddlewareInte
const oauthService = this.oauthInjectedService
const consumer = oauthService.getConsumer()
const oauthVerifier = request.query.oauth_verifier
const session = request.session
consumer.getOAuthAccessToken(
oauthService.requestTokenKey,
oauthService.requestTokenSecret,
@ -25,11 +26,15 @@ export default class OauthAccessTokenMiddleware implements ExpressMiddlewareInte
console.error(errorStr)
response.status(500).send('Error getting OAuth access token: ' + errorStr)
} else {
this.obpClientService.setAccessToken(oauthTokenKey, oauthTokenSecret)
response.redirect(
//`${process.env.VITE_OBP_EXPLORER_HOST}?key=${oauthTokenKey}&secret=${oauthTokenSecret}`
`${process.env.VITE_OBP_EXPLORER_HOST}`
)
const clientConfig = JSON.parse(
JSON.stringify(this.obpClientService.getOBPClientConfig())
) //Deep copy
clientConfig['oauthConfig']['accessToken'] = {
key: oauthTokenKey,
secret: oauthTokenSecret
}
session['clientConfig'] = clientConfig
response.redirect(`${process.env.VITE_OBP_EXPLORER_HOST}`)
}
}
)

View File

@ -30,23 +30,31 @@ export default class OBPClientService {
oauthConfig: this.oauthConfig
}
}
setAccessToken(key: string, secret: string): void {
this.oauthConfig['accessToken'] = { key, secret }
async get(path: string, clientConfig: any): Promise<any> {
const config = this.getSessionConfig(clientConfig)
return await get<API.Any>(config, Any)(GetAny)(path)
}
async get(path: string): Promise<any> {
return await get<API.Any>(this.clientConfig, Any)(GetAny)(path)
async create(path: string, body: any, clientConfig: any): Promise<any> {
const config = this.getSessionConfig(clientConfig)
return await create<API.Any>(config, Any)(CreateAny)(path)(body)
}
async create(path: string, body: any): Promise<any> {
return await create<API.Any>(this.clientConfig, Any)(CreateAny)(path)(body)
async update(path: string, body: any, clientConfig: any): Promise<any> {
const config = this.getSessionConfig(clientConfig)
return await update<API.Any>(config, Any)(UpdateAny)(path)(body)
}
async update(path: string, body: any): Promise<any> {
return await update<API.Any>(this.clientConfig, Any)(UpdateAny)(path)(body)
async discard(path: string, clientConfig: any): Promise<any> {
const config = this.getSessionConfig(clientConfig)
return await discard<API.Any>(config, Any)(DiscardAny)(path)
}
async discard(path: string): Promise<any> {
return await discard<API.Any>(this.clientConfig, Any)(DiscardAny)(path)
private getSessionConfig(clientConfig: any): any {
return clientConfig || this.clientConfig
}
getOBPVersion(): string {
return this.clientConfig.version
}
getOBPClientConfig(): any {
return this.clientConfig
}
}