![PyPI Now Supports iOS and Android Wheels for Mobile Python Development](https://cdn.sanity.io/images/cgdhsj6q/production/96416c872705517a6a65ad9646ce3e7caef623a0-1024x1024.webp?w=400&fit=max&auto=format)
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
@loopback/authentication-passport
Advanced tools
A package creating adapters between the passport module and @loopback/authentication
Important: We strongly recommend that users learn LoopBack's authentication system before using this module.
This is an adapter module created for plugging in
passport
based strategies to the
authentication system in @loopback/authentication@3.x
.
Experimental packages provide early access to advanced or experimental functionality to get community feedback. Such modules are published to npm using
0.x.y
versions. Their APIs and functionality may be subject to breaking changes in future releases.
npm i @loopback/authentication-passport --save
@loopback/authentication@3.x
allows users to register authentication
strategies that implement the interface
AuthenticationStrategy
Since AuthenticationStrategy
describes a strategy with different contracts
than the passport
Strategy
,
and we'd like to support the existing 500+ community passport strategies, an
adapter class is created in this package to convert a passport strategy to
the one that LoopBack 4 authentication system wants.
Taking the basic strategy exported from
passport-http
as an example,
first create an instance of the basic strategy with your verify
function.
// Create a file named `my-basic-auth-strategy.ts` to define your strategy below
import {BasicStrategy} from 'passport-http';
function verify(username: string, password: string, cb: Function) {
users.find(username, password, cb);
}
const basicStrategy = new BasicStrategy(verify);
It's a similar configuration as you add a strategy to a passport
by calling
passport.use()
.
// In file 'my-basic-auth-strategy.ts'
import {BasicStrategy} from 'passport-http';
function verify(username: string, password: string, cb: Function) {
users.find(username, password, cb);
}
const basicStrategy = new BasicStrategy(verify);
// Apply the adapter
export const AUTH_STRATEGY_NAME = 'basic';
export const basicAuthStrategy = new StrategyAdapter(
// The configured basic strategy instance
basicStrategy,
// Give the strategy a name
// You'd better define your strategy name as a constant, like
// `const AUTH_STRATEGY_NAME = 'basic'`.
// You will need to decorate the APIs later with the same name.
AUTH_STRATEGY_NAME,
);
import {Application, CoreTags} from '@loopback/core';
import {AuthenticationBindings} from '@loopback/authentication';
import {basicAuthStrategy} from './my-basic-auth-strategy';
app
.bind('authentication.strategies.basicAuthStrategy')
.to(basicAuthStrategy)
.tag({
[CoreTags.EXTENSION_FOR]:
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
});
To authenticate your request with the basic strategy, decorate your controller function like:
import {AUTH_STRATEGY_NAME} from './my-basic-auth-strategy';
class MyController {
constructor(
@inject(AuthenticationBindings.CURRENT_USER, {optional: true})
private user: UserProfile,
) {}
// Define your strategy name as a constant so that
// it is consistent with the name you provide in the adapter
@authenticate(AUTH_STRATEGY_NAME)
async whoAmI(): Promise<string> {
return this.user.id;
}
}
This part is same as registering a non-passport based strategy. Please make sure you follow the documentation adding-an-authentication-action-to-a-custom-sequence to rewrite your sequence. You can also find a sample implementation in this example tutorial.
If you need to inject stuff (e.g. the verify function) when configuring the strategy, you may want to provide your strategy as a provider.
Note: If you are not familiar with LoopBack providers, check the documentation in Extending LoopBack 4
Use passport-http
as the example again:
// Create a file named `my-basic-auth-strategy.ts` to define your strategy below
class PassportBasicAuthProvider implements Provider<AuthenticationStrategy> {
value(): AuthenticationStrategy {
// The code that returns the converted strategy
}
}
The Provider should have two functions:
A function that takes in the verify callback function and returns a configured
basic strategy. To know more about the configuration, please check
the configuration guide in module passport-http
.
A function that applies the StrategyAdapter
to the configured basic strategy
instance. Then in the value()
function, you return the converted strategy.
So a full implementation of the provider is:
// In file 'my-basic-auth-strategy.ts'
import {BasicStrategy, BasicVerifyFunction} from 'passport-http';
import {StrategyAdapter} from `@loopback/passport-adapter`;
import {AuthenticationStrategy} from '@loopback/authentication';
class PassportBasicAuthProvider implements Provider<AuthenticationStrategy> {
constructor(
@inject('authentication.basic.verify') verifyFn: BasicVerifyFunction,
);
value(): AuthenticationStrategy {
const basicStrategy = this.configuredBasicStrategy(verify);
return this.convertToAuthStrategy(basicStrategy);
}
// Takes in the verify callback function and returns a configured basic strategy.
configuredBasicStrategy(verifyFn: BasicVerifyFunction): BasicStrategy {
return new BasicStrategy(verifyFn);
}
// Applies the `StrategyAdapter` to the configured basic strategy instance.
// You'd better define your strategy name as a constant, like
// `const AUTH_STRATEGY_NAME = 'basic'`
// You will need to decorate the APIs later with the same name
convertToAuthStrategy(basic: BasicStrategy): AuthenticationStrategy {
return new StrategyAdapter(basic, AUTH_STRATEGY_NAME);
}
}
Register the strategy provider in your LoopBack application so that the authentication system can look for your strategy by name and invoke it:
// In the main file
import {addExtension} from '@loopback/core';
import {MyApplication} from '<path_to_your_app>';
import {PassportBasicAuthProvider} from '<path_to_the_provider>';
import {
AuthenticationBindings,
registerAuthenticationStrategy,
} from '@loopback/authentication';
const app = new MyApplication();
// In a real app the function would be imported from a community module
function verify(username: string, password: string, cb: Function) {
users.find(username, password, cb);
}
app.bind('authentication.basic.verify').to(verify);
registerAuthenticationStrategy(app, PassportBasicAuthProvider);
To authenticate your request with the basic strategy, decorate your controller function like:
import {AUTH_STRATEGY_NAME} from './my-basic-auth-strategy';
class MyController {
constructor(
@inject(AuthenticationBindings.CURRENT_USER) private user: UserProfile,
) {}
// Define your strategy name as a constant so that
// it is consistent with the name you provide in the adapter
@authenticate(AUTH_STRATEGY_NAME)
async whoAmI(): Promise<string> {
return this.user.id;
}
}
This part is same as registering a non-passport based strategy. Please make sure you follow the documentation adding-an-authentication-action-to-a-custom-sequence to rewrite your sequence. You can also find a sample implementation in this example tutorial.
FAQs
A package creating adapters between the passport module and @loopback/authentication
The npm package @loopback/authentication-passport receives a total of 1,244 weekly downloads. As such, @loopback/authentication-passport popularity was classified as popular.
We found that @loopback/authentication-passport demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.