loopback4-kratos
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

loopback4-kratos

NodeJS package

A simple Ory Kratos integration in loopback4 applications.

LoopBack

Installation

Install KratosComponent using npm;

$ [npm install | yarn add] loopback4-kratos

Basic Use

import {AuthenticationComponent} from '@loopback/authentication';

import {
  KratosComponentBindings,
  KratosComponent,
} from 'loopback4-kratos';

// ...

export class MyApplication extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options: ApplicationConfig = {}) {
    // ...

    this.component(AuthenticationComponent);

    this.component(KratosComponent);
    this.bind(KratosComponentBindings.CONFIG).to({
      baseUrl: 'http://kratos_url'
    });
    
    // To register a custom user service
    this.bind(KratosComponentBindings.USER_SERVICE.key).toClass(
      MyUserService
    );

    // ...
  }

  // ...
}

It is therefore necessary to define a new user service:

import {UserProfile} from '@loopback/security';

import {
  KratosUserService
} from 'loopback4-kratos';
import {Session} from '@ory/kratos-client';

// ...

export class MyUserService extends KratosUserService {
  convertToUserProfile(response: Session): UserProfile {
    const ans = super.convertToUserProfile(response);
    
    // Implement your strategy ...
    
    return ans;
  }

  // ...
}

After this, you can just use Kratos as authentication strategy across application.

import {authenticate} from '@loopback/authentication';
import {get} from '@loopback/rest';

// ...

export class YourController {
  @get('/foo')
  @authenticate('kratos')
  foo() {
    // this request is protected by kratos authentication
  }

  // ...
}

Package Sidebar

Install

npm i loopback4-kratos

Weekly Downloads

2

Version

1.0.1

License

GPL-3.0

Unpacked Size

65.9 kB

Total Files

50

Last publish

Collaborators

  • giuseppegrieco