@0xbutterfield/passport-siwe
v2.0.0
Published
Sign in with Ethereum and Passport
Downloads
6
Readme
@sloikaxyz/passport-siwe
Sign in with Ethereum and Passport
@sloikaxyz/passport-siwe
provides a Passport authentication strategy
for Sign in with Ethereum.
This module can be used with Express, Nest.js, Fastify
(via @fastify/passport
), or any other framework that supports Connect-style middleware.
Usage
Client-facing API
The strategy expects the request body to contain an object with keys message
— a plaintext message conforming to the EIP-4361 spec, and signature
— the signature over the message, represented as a hex string. For an example of how to generate this on frontend, see siwe usage examples.
Configuring the strategy
import Strategy from '@sloikaxyz/passport-siwe';
import { SiweMessage } from 'siwe';
passport.use(
new Strategy(
{
domain: 'example.org', // authentication domain, required
},
function (
message: SiweMessage,
callback: (
err: Error | null,
user: unknown,
info?: unknown,
) => void,
) {
// The callback is invoked if the message is valid, i.e.:
// - the authentication domain matches,
// - the message is in effect (see "Expiration Time" and "Not Before"),
// - the signature provided in the request is valid.
//
// NOTE: Nonce is not validated by the strategy — it's up to you to implement that.
//
// Use `message.address` — an EIP-55 checksummed address — to determine the user.
//
// Call...
// - callback(null, user) to sign the user in
// - callback(null, null) to fail this authentication attempt
// - callback(err, null) to signal that a server-side error has occurred while processing this request
},
),
);
Accessing the request object
import Strategy from '@sloikaxyz/passport-siwe';
import express from 'express';
import { SiweMessage } from 'siwe';
passport.use(
new Strategy(
{
domain: 'example.org',
passReqToCallback: true, // pass the request as the first argument of the callback
},
function (
req: express.Request, // an express-style request object
message: SiweMessage,
callback: (
err: Error | null,
user: unknown,
info?: unknown,
) => void,
) {
// See "Basic usage" for discussion of the callback.
},
),
);
Installation
Install my-project with npm
npm install @sloikaxyz/passport-siwe siwe@^2.0.5 ethers@^5.6.8
# or
yarn add @sloikaxyz/passport-siwe siwe@^2.0.5 ethers@^5.6.8
Roadmap
- Nonce verification support
Contributing
Contributions are always welcome!
Quick steps:
- Fork and clone the repo.
- Make changes to the strategy as necessary — you can run
yarn test:watch
while developing. - Commit the changes and run
yarn test:ci
to verify the tests pass. - Send a PR :)
Acknowledgements
- EIP-4361: Sign-In with Ethereum, the spec
- siwe, the Sign in with Ethereum library that does most of the heavy-lifting here
- Passport, the Node.js authentication library
- passport-ethereum, a
[email protected]
Passport strategy from the author of Passport
License
MIT License
Copyright (c) 2022 Sloika Industries, Inc.
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.