Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ api-docs
**/*.tgz
packages/*/dist
examples/*/dist
experimental/*/dist
benchmark/dist
**/package
.sandbox
Expand Down
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ packages/cli/generators/*/templates
**/.sandbox
packages/*/dist
examples/*/dist
experimental/*/dist
benchmark/dist
sandbox/**/*
*.json
Expand Down
42 changes: 42 additions & 0 deletions bin/mark-non-lab-packages-private.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env node
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: loopback-next
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

/**
* This is an internal script to set all non-labs packages to be private
*/
'use strict';

const path = require('path');
const fs = require('fs');

const Project = require('@lerna/project');

async function markNonLabsPackagesPrivate() {
const project = new Project(process.cwd());
const packages = await project.getPackages();

// Set `"private": "true"` in individual packages
for (const pkg of packages) {
const dir = path.relative(pkg.rootPath, pkg.location);
if (dir.startsWith('labs')) continue;
const data = readJsonFile(pkg.manifestLocation);
let modified = !data.private;
if (!modified) continue;
data.private = true;
writeJsonFile(pkg.manifestLocation, data, dir);
}
}

if (require.main === module) markNonLabsPackagesPrivate();

function readJsonFile(filePath) {
return JSON.parse(fs.readFileSync(filePath, 'utf-8'));
}

function writeJsonFile(filePath, data, dir) {
fs.writeFileSync(filePath, JSON.stringify(data, null, 2) + '\n', 'utf-8');
console.log('> %s has been marked as private.', dir || filePath);
}
3 changes: 2 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
"license": "MIT",
"dependencies": {
"fs-extra": "^8.0.0"
}
},
"private": true
}
3 changes: 2 additions & 1 deletion examples/express-composition/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,6 @@
"@types/node": "^10.11.2",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
}
},
"private": true
}
3 changes: 2 additions & 1 deletion examples/greeter-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,6 @@
"@loopback/rest": "^1.12.0",
"chalk": "^2.4.2",
"debug": "^4.0.1"
}
},
"private": true
}
3 changes: 2 additions & 1 deletion examples/hello-world/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@
"LoopBack",
"example",
"tutorial"
]
],
"private": true
}
3 changes: 2 additions & 1 deletion examples/log-extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@
"@loopback/rest": "^1.12.0",
"chalk": "^2.3.2",
"debug": "^4.0.1"
}
},
"private": true
}
3 changes: 2 additions & 1 deletion examples/rpc-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,6 @@
"@types/node": "^10.11.2",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
}
},
"private": true
}
3 changes: 2 additions & 1 deletion examples/soap-calculator/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@
"source-map-support": "^0.5.5",
"tslint": "^5.16.0",
"typescript": "^3.4.5"
}
},
"private": true
}
3 changes: 2 additions & 1 deletion examples/todo-list/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,6 @@
"models",
"todo",
"HasMany"
]
],
"private": true
}
3 changes: 2 additions & 1 deletion examples/todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,6 @@
"CRUD",
"models",
"todo"
]
],
"private": true
}
1 change: 1 addition & 0 deletions experimental/passport-adapter/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=true
25 changes: 25 additions & 0 deletions experimental/passport-adapter/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2017,2019. All Rights Reserved.
Node module: @loopback/authentication
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
111 changes: 111 additions & 0 deletions experimental/passport-adapter/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
## Passport Adapter

*Important: We suggest users understand LoopBack's authentication system[Link TBD](some loopback.io link) before using this module*

This is an adapter module created for plugging in [`passport`](https://www.npmjs.com/package/passport) base strategies to the authentication system in `@loopback/authentication@2.x`.

## Installation

```sh
npm i @loopback/passport-adapter --save
```

## Background

`@loopback/authentication@2.x` allows users to register authentication strategies that implement the interface [`AuthenticationStrategy`](https://apidocs.strongloop.com/@loopback%2fdocs/authentication.html#AuthenticationStrategy)

Since `AuthenticationStrategy` describes a strategy with different contracts than the passport [`Strategy`](https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/passport/index.d.ts#L79), 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.

## Usage

1. Create a provider for the strategy

Take the basic strategy exported from [`passport-http`](https://github.com/jaredhanson/passport-http) as an example, create a provider that returns a converted basic strategy:

*Note: If you are not familiar with LoopBack provider, check the documentations in [Extending LoopBack 4](https://loopback.io/doc/en/lb4/Extending-LoopBack-4.html)*

```ts
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`](https://github.com/jaredhanson/passport-http#usage-of-http-basic).

- 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:

```ts
import {BasicStrategy, BasicVerifyFunction} from 'passport-http';
import {StrategyAdapter} from `@loopback/passport-adapter`;
import {AuthenticationStrategy} from '@loopback/authentication';

class PassportBasicAuthProvider implements Provider<AuthenticationStrategy> {
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'`
// So that you will decorate the APIs later with the same name
convertToAuthStrategy(basic: BasicStrategy): AuthenticationStrategy {
return new StrategyAdapter(basic, AUTH_STRATEGY_NAME);
}
}
```

2. Register the strategy provider

Register the strategy provider in your LoopBack application so that the authentication system can look for your strategy by name and invoke it:

```ts
// In the main file

import {addExtension} from '@loopback/core';
import {MyApplication} from '<path_to_your_app>';
import {AuthenticationBindings} from '@loopback/authentication';

const app = new MyApplication();

addExtension(
app,
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
PassportBasicAuthProvider,
{
namespace:
AuthenticationBindings.AUTHENTICATION_STRATEGY_EXTENSION_POINT_NAME,
},
);
```

3. Decorate your endpoint

To authenticate your request with the basic strategy, decorate your controller function like:

```ts
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;
}
}
```

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
Loading