authorization with JWT seems to work

This commit is contained in:
Moriz Wahl 2021-08-26 22:49:33 +02:00
parent 8ec55b0a43
commit 7eec6faace
4 changed files with 40 additions and 9 deletions

13
backend/src/auth/auth.ts Normal file
View File

@ -0,0 +1,13 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import { AuthChecker } from 'type-graphql'
import decode from '../jwt/decode'
/* eslint-disable-next-line @typescript-eslint/no-unused-vars */
export const isAuthorized: AuthChecker<any> = ({ root, args, context, info }, roles) => {
if (context.token) {
const decoded = decode(context.token)
if (decoded.sessionId && decoded.sessionId !== 0) return true
}
return false
}

View File

@ -1,4 +1,4 @@
import { Resolver, Query, Args } from 'type-graphql'
import { Resolver, Query, Args, Authorized } from 'type-graphql'
import CONFIG from '../../config'
import { TransactionList } from '../models/Transaction'
import { TransactionListInput, TransactionSendArgs } from '../inputs/TransactionInput'
@ -6,6 +6,7 @@ import { apiGet, apiPost } from '../../apis/loginAPI'
@Resolver()
export class TransactionResolver {
@Authorized()
@Query(() => TransactionList)
async transactionList(
@Args() { sessionId, firstPage = 1, items = 25, order = 'DESC' }: TransactionListInput,
@ -17,6 +18,7 @@ export class TransactionResolver {
return new TransactionList(result.data)
}
@Authorized()
@Query(() => String)
async sendCoins(
@Args() { sessionId, email, amount, memo }: TransactionSendArgs,

View File

@ -1,3 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
import 'reflect-metadata'
import express from 'express'
import { buildSchema } from 'type-graphql'
@ -13,11 +15,25 @@ import { BalanceResolver } from './graphql/resolvers/BalanceResolver'
import { GdtResolver } from './graphql/resolvers/GdtResolver'
import { TransactionResolver } from './graphql/resolvers/TransactionResolver'
import { isAuthorized } from './auth/auth'
// TODO implement
// import queryComplexity, { simpleEstimator, fieldConfigEstimator } from "graphql-query-complexity";
const DB_VERSION = '0001-init_db'
const context = (req: any) => {
const authorization = req.req.headers.authorization
let token = null
if (authorization) {
token = req.req.headers.authorization.replace(/^Bearer /, '')
}
const context = {
token,
}
return context
}
async function main() {
// check for correct database version
const con = await connection()
@ -33,6 +49,7 @@ async function main() {
// const connection = await createConnection()
const schema = await buildSchema({
resolvers: [UserResolver, BalanceResolver, TransactionResolver, GdtResolver],
authChecker: isAuthorized,
})
// Graphiql interface
@ -45,7 +62,7 @@ async function main() {
const server = express()
// Apollo Server
const apollo = new ApolloServer({ schema, playground })
const apollo = new ApolloServer({ schema, playground, context })
apollo.applyMiddleware({ app: server })
// Start Server

View File

@ -2,17 +2,16 @@
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import jwt from 'jsonwebtoken'
import '../config'
import CONFIG from '../config/'
export default async (authorizationHeader: string): any => {
if (!authorizationHeader) return null
const token = authorizationHeader.replace('Bearer ', '')
export default (token: string): any => {
if (!token) return null
let sessionId = null
let email = null
const email = null
try {
const decoded = await jwt.verify(token, CONFIG.JWT_SECRET)
const decoded = jwt.verify(token, CONFIG.JWT_SECRET)
sessionId = decoded.sub
email = decoded.email
// email = decoded.email
} catch (err) {
return null
}