diff --git a/backend/src/application.ts b/backend/src/application.ts index dab8732..ab403bd 100644 --- a/backend/src/application.ts +++ b/backend/src/application.ts @@ -1,5 +1,5 @@ import { BootMixin } from '@loopback/boot'; -import { Application, ApplicationConfig } from '@loopback/core'; +import { Application, ApplicationConfig, BindingScope } from '@loopback/core'; import { RepositoryMixin } from '@loopback/repository'; import { ServiceMixin } from '@loopback/service-proxy'; import { AuthenticationBindings, AuthenticationComponent } from '@loopback/authentication'; @@ -9,9 +9,10 @@ import { TokenServiceBindings, UserServiceBindings } from '@loopback/authentication-jwt'; -import { SzakdolgozatUserService } from './services'; +import { AuthService, SzakdolgozatUserService } from './services'; import { GraphQLBindings, GraphQLServer } from '@loopback/graphql'; import { UserResolver } from './graphql-resolvers/user-resolver'; +import { SzakdolgozatBindings } from './bindings'; export { ApplicationConfig }; @@ -36,14 +37,11 @@ export class SzakdolgozatBackendApplication extends BootMixin( this.get(TokenServiceBindings.TOKEN_SERVICE).then(tokenService => { this.bind(AuthenticationBindings.STRATEGY).to(new JWTAuthenticationStrategy(tokenService)); }); - this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER).to(async (resolverData, roles) => { - const authenticate = await this.get(AuthenticationBindings.AUTH_ACTION); - const res = await authenticate(( resolverData.context).req); - console.log('Res: ', res); - return true; - }); this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE); + this.service(AuthService, {defaultScope: BindingScope.REQUEST, key: SzakdolgozatBindings.AUTH_SERVICE}); + this.get(SzakdolgozatBindings.AUTH_SERVICE).then(service => this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER) + .to((resolverData, roles) => service.authUser(resolverData, roles))); this.projectRoot = __dirname; this.bootOptions = { diff --git a/backend/src/bindings.ts b/backend/src/bindings.ts new file mode 100644 index 0000000..c6087dc --- /dev/null +++ b/backend/src/bindings.ts @@ -0,0 +1,7 @@ +import { BindingKey } from '@loopback/core'; +import { AuthService } from './services'; + +export namespace SzakdolgozatBindings { + export const AUTH_SERVICE = BindingKey.create('szakdolgozat.auth'); + //export const AUTH_TOKEN = BindingKey.create('szakdolgozat.auth.token'); +} diff --git a/backend/src/graphql-resolvers/user-resolver.ts b/backend/src/graphql-resolvers/user-resolver.ts index 5adabb9..d56d5c5 100644 --- a/backend/src/graphql-resolvers/user-resolver.ts +++ b/backend/src/graphql-resolvers/user-resolver.ts @@ -3,7 +3,7 @@ import { User } from '../models'; import { repository } from '@loopback/repository'; import { UserRepository } from '../repositories'; import { inject } from '@loopback/core'; -import { SzakdolgozatUserService } from '../services'; +import { AuthService, SzakdolgozatUserService } from '../services'; import { TokenServiceBindings, UserServiceBindings } from '@loopback/authentication-jwt'; import { TokenService } from '@loopback/authentication'; import { SecurityBindings, UserProfile } from '@loopback/security'; @@ -12,6 +12,7 @@ import { UserRegisterInput } from '../graphql-types/input/user-register.input'; import { validated } from '../helpers'; import { LoginResult } from '../graphql-types/user'; import { UserUpdateInput } from '../graphql-types/input/user-update.input'; +import { SzakdolgozatBindings } from '../bindings'; @resolver(of => User) export class UserResolver { @@ -20,8 +21,10 @@ export class UserResolver { @inject(UserServiceBindings.USER_SERVICE) private readonly userService: SzakdolgozatUserService, @inject(GraphQLBindings.RESOLVER_DATA) private readonly resolverData: ResolverData, @inject(TokenServiceBindings.TOKEN_SERVICE) public jwtService: TokenService, - @inject(SecurityBindings.USER, {optional: true}) public user: UserProfile + @inject(SecurityBindings.USER, {optional: true}) public user: UserProfile, + @inject(SzakdolgozatBindings.AUTH_SERVICE) private authService: AuthService ) { + console.log('Auth service', authService); } @mutation(returns => User) @@ -55,16 +58,10 @@ export class UserResolver { @authorized() @mutation(returns => Boolean) - async logout(@inject(GraphQLBindings.RESOLVER_DATA) request: any): Promise { - console.log('request:', request); //TODO - const split = request.headers.get('Authorization')?.split(' '); - if (split && split.length > 1) { - if (this.jwtService.revokeToken) { - await this.jwtService.revokeToken(split[1]); - } else { - console.error('Cannot revoke token'); - } - } + async logout(): Promise { + console.log('Logout service: ', this.authService); + console.log('token:', this.authService.receivedToken); //TODO + await this.jwtService.revokeToken?.(this.authService.receivedToken); return true; } diff --git a/backend/src/services/auth.service.ts b/backend/src/services/auth.service.ts new file mode 100644 index 0000000..b75dd53 --- /dev/null +++ b/backend/src/services/auth.service.ts @@ -0,0 +1,28 @@ +import { AuthenticateFn, AuthenticationBindings, AuthenticationStrategy } from '@loopback/authentication'; +import { ExpressContext, ResolverData } from '@loopback/graphql'; +import { Getter, inject } from '@loopback/core'; +import { JWTAuthenticationStrategy } from '@loopback/authentication-jwt'; + +export class AuthService { + receivedToken: string; + + constructor(@inject(AuthenticationBindings.AUTH_ACTION) private authenticate: AuthenticateFn, + @inject.getter(AuthenticationBindings.STRATEGY) + readonly getStrategies: Getter) { + console.log('Created auth service', new Error().stack); + } + + async authUser(resolverData: ResolverData, roles: string[]) { + const context = ( resolverData.context); + const res = await this.authenticate(context.req); + const strat = await this.getStrategies(); + // Itt már biztosan van érvényes token + console.log('This: ', this); + this.receivedToken = strat.extractCredentials(context.req); + console.log('This: ', this); + console.log('Strat: ', strat); + console.log('Creds: ', strat.extractCredentials(context.req)); + console.log('Res: ', ( res).id); + return true; + } +} diff --git a/backend/src/services/index.ts b/backend/src/services/index.ts index e17ee5c..d99a6a5 100644 --- a/backend/src/services/index.ts +++ b/backend/src/services/index.ts @@ -1 +1,2 @@ export * from './user.service'; +export * from './auth.service';