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,
UserServiceBindings
} from '@loopback/authentication-jwt';
import { AuthService, SzakdolgozatUserService } from './services';
import { SzakdolgozatUserService } from './services';
import { GraphQLBindings, GraphQLServer } from '@loopback/graphql';
import { UserResolver } from './graphql-resolvers/user-resolver';
import { SzakdolgozatBindings } from './bindings';
import { SzakdolgozatAuthChecker } from './szakdolgozat-auth-checker';
export { ApplicationConfig };
@ -38,10 +37,9 @@ 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).toProvider(SzakdolgozatAuthChecker);
this.bind(GraphQLBindings.GRAPHQL_AUTH_CHECKER).toDynamicValue(SzakdolgozatAuthChecker);
this.service(SzakdolgozatUserService, UserServiceBindings.USER_SERVICE);
this.service(AuthService, SzakdolgozatBindings.AUTH_SERVICE);
this.projectRoot = __dirname;
this.bootOptions = {

View file

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

View file

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

View file

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