Logout implementation attempt

This commit is contained in:
Norbi Peti 2022-04-27 02:24:24 +02:00
parent 8b2efc0997
commit 076249f088
No known key found for this signature in database
GPG key ID: DBA4C4549A927E56
5 changed files with 51 additions and 20 deletions

View file

@ -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((<any> 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 = {

7
backend/src/bindings.ts Normal file
View file

@ -0,0 +1,7 @@
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

@ -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<boolean> {
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<boolean> {
console.log('Logout service: ', this.authService);
console.log('token:', this.authService.receivedToken); //TODO
await this.jwtService.revokeToken?.(this.authService.receivedToken);
return true;
}

View file

@ -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<AuthenticationStrategy | AuthenticationStrategy[] | undefined>) {
console.log('Created auth service', new Error().stack);
}
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('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: ', (<any> res).id);
return true;
}
}

View file

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