Logout implementation complete

Revoking the token isn't even possible atm
This commit is contained in:
Norbi Peti 2022-04-27 04:13:19 +02:00
parent 495f0b1600
commit d60a9870b3
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
6 changed files with 26 additions and 43 deletions

View file

@ -9,10 +9,9 @@ import {
TokenServiceBindings, TokenServiceBindings,
UserServiceBindings UserServiceBindings
} from '@loopback/authentication-jwt'; } from '@loopback/authentication-jwt';
import { AuthService, SzakdolgozatUserService } from './services'; import { SzakdolgozatUserService } from './services';
import { GraphQLBindings, GraphQLServer } from '@loopback/graphql'; import { GraphQLBindings, GraphQLServer } from '@loopback/graphql';
import { UserResolver } from './graphql-resolvers/user-resolver'; import { UserResolver } from './graphql-resolvers/user-resolver';
import { SzakdolgozatBindings } from './bindings';
import { SzakdolgozatAuthChecker } from './szakdolgozat-auth-checker'; import { SzakdolgozatAuthChecker } from './szakdolgozat-auth-checker';
export { ApplicationConfig }; export { ApplicationConfig };
@ -38,10 +37,9 @@ export class SzakdolgozatBackendApplication extends BootMixin(
this.get(TokenServiceBindings.TOKEN_SERVICE).then(tokenService => { this.get(TokenServiceBindings.TOKEN_SERVICE).then(tokenService => {
this.bind(AuthenticationBindings.STRATEGY).to(new JWTAuthenticationStrategy(tokenService)); this.bind(AuthenticationBindings.STRATEGY).to(new JWTAuthenticationStrategy(tokenService));
}); });
this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER).toProvider(SzakdolgozatAuthChecker); this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER).toDynamicValue(SzakdolgozatAuthChecker);
this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE); this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE);
this.service(AuthService, SzakdolgozatBindings.AUTH_SERVICE);
this.projectRoot = __dirname; this.projectRoot = __dirname;
this.bootOptions = { this.bootOptions = {

View file

@ -1,7 +1,5 @@
import { BindingKey } from '@loopback/core'; import { BindingKey } from '@loopback/core';
import { AuthService } from './services';
export namespace SzakdolgozatBindings { export namespace SzakdolgozatBindings {
export const AUTH_SERVICE = BindingKey.create<AuthService>('szakdolgozat.auth');
export const AUTH_TOKEN = BindingKey.create<string>('szakdolgozat.auth_token'); export const AUTH_TOKEN = BindingKey.create<string>('szakdolgozat.auth_token');
} }

View file

@ -57,11 +57,13 @@ export class UserResolver {
@authorized() @authorized()
@mutation(returns => Boolean) @mutation(returns => Boolean)
async logout(@inject(SzakdolgozatBindings.AUTH_TOKEN) token: string): Promise<boolean> { async logout(): Promise<boolean> {
console.log('Logout service: ', token); const token = await this.context.get(SzakdolgozatBindings.AUTH_TOKEN);
console.log('Context: ', this.context?.name); if (this.jwtService.revokeToken) {
//console.log('token:', this.authService.receivedToken); //TODO await this.jwtService.revokeToken(token);
//await this.jwtService.revokeToken?.(this.authService.receivedToken); } else {
console.error('Cannot revoke token');
}
return true; return true;
} }

View file

@ -1,7 +0,0 @@
export class AuthService {
receivedToken: string;
constructor() {
console.log('new auth service');
}
}

View file

@ -1,2 +1 @@
export * from './user.service'; export * from './user.service';
export * from './auth.service';

View file

@ -1,32 +1,25 @@
import { AuthenticateFn, AuthenticationBindings, AuthenticationStrategy } from '@loopback/authentication'; import { AuthenticateFn, AuthenticationBindings, AuthenticationStrategy } from '@loopback/authentication';
import { AuthChecker, ExpressContext, ResolverData } from '@loopback/graphql'; import { AuthChecker, ExpressContext } from '@loopback/graphql';
import { Context, Getter, inject, Provider, ValueOrPromise } from '@loopback/core'; import { Context, Getter, inject, ValueOrPromise } from '@loopback/core';
import { JWTAuthenticationStrategy } from '@loopback/authentication-jwt'; import { JWTAuthenticationStrategy } from '@loopback/authentication-jwt';
import { SzakdolgozatBindings } from './bindings'; import { SzakdolgozatBindings } from './bindings';
import { AuthService } from './services';
export class SzakdolgozatAuthChecker implements Provider<AuthChecker> { export class SzakdolgozatAuthChecker {
constructor(@inject(AuthenticationBindings.AUTH_ACTION) private authenticate: AuthenticateFn, constructor() {
@inject.getter(AuthenticationBindings.STRATEGY) console.log('New auth checker');
readonly getStrategies: Getter<AuthenticationStrategy | AuthenticationStrategy[] | undefined>,
@inject(SzakdolgozatBindings.AUTH_SERVICE) private authService: AuthService,
@inject.context() private context: Context) {
console.log('new auth checker');
} }
value(): ValueOrPromise<AuthChecker> { static value(@inject(AuthenticationBindings.AUTH_ACTION) authenticate: AuthenticateFn,
return this.authUser.bind(this); @inject.getter(AuthenticationBindings.STRATEGY)
} getStrategies: Getter<AuthenticationStrategy | AuthenticationStrategy[] | undefined>,
@inject.context() context: Context): ValueOrPromise<AuthChecker> {
async authUser(resolverData: ResolverData, roles: string[]) { return async (resolverData, roles) => {
const context = (<ExpressContext> resolverData.context); const econtext = (<ExpressContext> resolverData.context);
const res = await this.authenticate(context.req); const res = await authenticate(econtext.req);
const strat = <JWTAuthenticationStrategy> await this.getStrategies(); const strat = <JWTAuthenticationStrategy> await getStrategies();
// Itt már biztosan van érvényes token // Itt már biztosan van érvényes token
console.log('Context: ', this.context.name); context.bind(SzakdolgozatBindings.AUTH_TOKEN).to(strat.extractCredentials(econtext.req));
this.context.bind(SzakdolgozatBindings.AUTH_TOKEN).to(strat.extractCredentials(context.req)); return true;
console.log('This: ', this.authService); };
console.log('Res: ', (<any> res).id);
return true;
} }
} }