Skip to content

Commit

Permalink
feat: add provider and templates for adonisjs
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Sep 6, 2019
1 parent 9dc3d2e commit 05f06fa
Show file tree
Hide file tree
Showing 9 changed files with 223 additions and 27 deletions.
55 changes: 31 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<div align="center">
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1557762307/poppinss_iftxlt.jpg" width="600px">
<img src="https://res.cloudinary.com/adonisjs/image/upload/q_100/v1564392111/adonis-banner_o9lunk.png" width="600px">
</div>

# Password hashing
Expand All @@ -15,8 +15,8 @@ This module is used by [AdonisJs](https://adonisjs.com) to hash user password wi

- [Features](#features)
- [Usage](#usage)
- [Using with AdonisJs](#using-with-adonisjs)
- [Switching drivers](#switching-drivers)
- [Adding custom drivers](#adding-custom-drivers)
- [API Docs](#api-docs)
- [Maintainers](#maintainers)

Expand All @@ -28,51 +28,58 @@ This module is used by [AdonisJs](https://adonisjs.com) to hash user password wi
3. Wraps the hash output to a [PHC string format](https://github.com/P-H-C/phc-string-format/blob/master/phc-sf-spec.md), this allows upgrading user passwords, when the underlying configuration changes.

## Usage
> The bcrypt driver doesn't work with Node v12 yet. We are waiting for bcrypt open issues to get fixed.

Install the package from npm registry as follows:

```sh
npm i @poppinss/hash
npm i @adonisjs/hash

# yarn
yarn add @poppinss/hash
yarn add @adonisjs/hash
```

and then use it as follows:

```ts
import { Hash } from '@poppinss/hash'
const hash = new Hash(iocContainer, config)
import { Hash } from '@adonisjs/hash/build/standalone'
const hash = new Hash({}, config)

const hashedValue = await hash.hash('password')
await hash.verify(hashedValue)

await hash.needsRehash(hashedValue) // false
```

## Switching drivers
You can switch drivers using the `driver` method.
## Using with AdonisJs
The `@adonisjs/core` module includes this module by default. However, here's how you can set it up manually.

```ts
import { Hash } from '@poppinss/hash'
const hash = new Hash(iocContainer, config)
const providers = [
'@adonisjs/hash/build/providers/HashProvider'
]
```

await hash.driver('bcrypt').hash('password')
And then also register the typings file inside `tsconfig.json` file.

```json
{
"files": ["./node_modules/@adonisjs/hash/build/adonis-typings/hash.d.ts"]
}
```

## Adding custom drivers
The custom drivers can be added using the `extend` method.
And use it as follows:

```ts
import { Hash, HashDriverContract } from '@poppinss/hash'
const hash = new Hash(iocContainer, config)
import Hash from '@ioc:Adonis/Core/Hash'
await Hash.hash('password')
```

class Scrypt implements HashDriverContract {}

hash.extend('scrypt', (container) => {
return new Scrypt()
})
## Switching drivers
You can switch drivers using the `use` method.

```ts
await Hash.use('bcrypt').hash('password')
```

## API Docs
Expand All @@ -83,11 +90,11 @@ Following are the autogenerated files via Typedoc
## Maintainers
[Harminder virk](https://github.com/thetutlage)

[circleci-image]: https://img.shields.io/circleci/project/github/poppinss/hash/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/poppinss/hash "circleci"
[circleci-image]: https://img.shields.io/circleci/project/github/adonisjs/hash/master.svg?style=for-the-badge&logo=circleci
[circleci-url]: https://circleci.com/gh/adonisjs/hash "circleci"

[npm-image]: https://img.shields.io/npm/v/@poppinss/hash.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@poppinss/hash "npm"
[npm-image]: https://img.shields.io/npm/v/@adonisjs/hash.svg?style=for-the-badge&logo=npm
[npm-url]: https://npmjs.org/package/@adonisjs/hash "npm"

[typescript-image]: https://img.shields.io/badge/Typescript-294E80.svg?style=for-the-badge&logo=typescript

Expand Down
4 changes: 2 additions & 2 deletions adonis-typings/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ declare module '@ioc:Adonis/Core/Hash' {
}

/**
* A list of default mappings. One must extend this interface to add
* their own
* List of hash mappings used by the app. Using declaration merging, one
* must extend this interface
*/
export interface HashList {
}
Expand Down
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
### External modules

* ["adonis-typings/hash"](modules/_adonis_typings_hash_.md)
* ["providers/HashProvider"](modules/_providers_hashprovider_.md)
* ["src/Drivers/Argon"](modules/_src_drivers_argon_.md)
* ["src/Drivers/Bcrypt"](modules/_src_drivers_bcrypt_.md)
* ["src/Hash"](modules/_src_hash_.md)
Expand Down
51 changes: 51 additions & 0 deletions docs/classes/_providers_hashprovider_.configprovider.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
**[@adonisjs/hash](../README.md)**

[Globals](../README.md)["providers/HashProvider"](../modules/_providers_hashprovider_.md)[ConfigProvider](_providers_hashprovider_.configprovider.md)

# Class: ConfigProvider

## Hierarchy

* **ConfigProvider**

## Index

### Constructors

* [constructor](_providers_hashprovider_.configprovider.md#constructor)

### Properties

* [$container](_providers_hashprovider_.configprovider.md#protected-$container)

### Methods

* [register](_providers_hashprovider_.configprovider.md#register)

## Constructors

### constructor

\+ **new ConfigProvider**(`$container`: IocContract): *[ConfigProvider](_providers_hashprovider_.configprovider.md)*

**Parameters:**

Name | Type |
------ | ------ |
`$container` | IocContract |

**Returns:** *[ConfigProvider](_providers_hashprovider_.configprovider.md)*

## Properties

### `Protected` $container

**$container**: *IocContract*

## Methods

### register

**register**(): *void*

**Returns:** *void*
11 changes: 11 additions & 0 deletions docs/modules/_providers_hashprovider_.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
**[@adonisjs/hash](../README.md)**

[Globals](../README.md)["providers/HashProvider"](_providers_hashprovider_.md)

# External module: "providers/HashProvider"

## Index

### Classes

* [ConfigProvider](../classes/_providers_hashprovider_.configprovider.md)
16 changes: 15 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"files": [
"build/adonis-typings",
"build/providers",
"build/templates",
"build/src",
"build/standalone.d.ts",
"build/standalone.js"
Expand All @@ -16,7 +17,8 @@
"lint": "tslint --project tsconfig.json",
"clean": "del build",
"compile": "npm run lint && npm run clean && tsc",
"build": "npm run compile && typedoc --theme markdown --excludePrivate --hideSources && git add docs",
"copyfiles": "copyfiles \"templates/**/*.txt\" build",
"build": "npm run compile && npm run copyfiles && typedoc --theme markdown --excludePrivate --hideSources && git add docs",
"commit": "git-cz",
"release": "np",
"version": "npm run build"
Expand All @@ -37,11 +39,13 @@
},
"homepage": "https://github.com/poppinss/hash#readme",
"devDependencies": {
"@adonisjs/fold": "^6.1.6",
"@adonisjs/mrm-preset": "^2.1.0",
"@phc/argon2": "^1.0.9",
"@phc/bcrypt": "^1.0.2",
"@types/node": "^12.7.4",
"commitizen": "^4.0.3",
"copyfiles": "^2.1.1",
"cz-conventional-changelog": "^3.0.2",
"del-cli": "^2.0.0",
"doctoc": "^1.4.0",
Expand Down Expand Up @@ -83,5 +87,15 @@
"dependencies": {
"@phc/format": "^0.5.0",
"@poppinss/manager": "^2.0.0"
},
"adonisjs": {
"templates": {
"config": [
"config/hash.txt"
],
"contracts": [
"contracts/hash.txt"
]
}
}
}
26 changes: 26 additions & 0 deletions providers/HashProvider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @module @adonisjs/hash
*/

/*
* @adonisjs/hash
*
* (c) Harminder Virk <virk@adonisjs.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

import { IocContract } from '@adonisjs/fold'
import { Hash } from '../src/Hash'

export default class ConfigProvider {
constructor (protected $container: IocContract) {}

public register () {
this.$container.singleton('Adonis/Core/Hash', () => {
const config = this.$container.use('Adonis/Core/Config').get('hash', {})
return new Hash(this, config)
})
}
}
74 changes: 74 additions & 0 deletions templates/config/hash.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/**
* Config source: https://git.io/fj1Kb
*
* Feel free to let us know via PR, if you find something broken in this config
* file.
*/

import { HashConfigContract } from '@ioc:Adonis/Core/Hash'

/*
|--------------------------------------------------------------------------
| Hash Config
|--------------------------------------------------------------------------
|
| The `HashConfigContract` relies on the `HashList` interface which is
| defined inside `contracts` directory.
|
*/
const hashConfig: HashConfigContract = {
/*
|--------------------------------------------------------------------------
| Default mapping
|--------------------------------------------------------------------------
|
| By default we make use of the bcrypt mapping to hash values. However, feel
| free to change the default value
|
*/
default: 'bcrypt',

list: {
/*
|--------------------------------------------------------------------------
| Argon
|--------------------------------------------------------------------------
|
| Argon mapping uses the `argon2` driver to hash values.
|
| Make sure you install the underlying dependency for this driver to work.
| https://www.npmjs.com/package/@phc/argon2.
|
| npm install @phc/argon2@"<2.0.0"
|
*/
argon: {
driver: 'argon2',
variant: 'id',
iterations: 3,
memory: 4096,
parallelism: 1,
saltSize: 16,
},

/*
|--------------------------------------------------------------------------
| Bcrypt
|--------------------------------------------------------------------------
|
| Bcrypt mapping uses the `bcrypt` driver to hash values.
|
| Make sure you install the underlying dependency for this driver to work.
| https://www.npmjs.com/package/@phc/bcrypt.
|
| npm install @phc/bcrypt@"<2.0.0"
|
*/
bcrypt: {
driver: 'bcrypt',
rounds: 10,
},
},
}

export default hashConfig
12 changes: 12 additions & 0 deletions templates/contracts/contract.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
declare module '@ioc:Adonis/Core/Hash' {
interface HashList {
bcrypt: {
config: BcryptConfigContract,
implementation: BcryptContract,
},
argon: {
config: ArgonConfigContract,
implementation: ArgonContract,
}
}
}

0 comments on commit 05f06fa

Please sign in to comment.