Crossmint / solana-auth

Sign-in with Solana wallet, with server+client side authentication and persistent sessions
Apache License 2.0
45 stars 7 forks source link

Implement Solana Sign-In #1

Open alfonso-paella opened 2 years ago

alfonso-paella commented 2 years ago

We're looking to implement Solana sign-in for our project.

We need a client-side Javascript library + a server side nodejs library that implement sign-in with Solana, and use Firebase Auth for persistent auth sessions after sign in.

Here's the full list of steps we expect this to entail:

  1. Create a sign-in entry point

  2. When user initiates sign-in: 1.1 User "connects" with their wallet on the browser, via wallet adapter, and we get their pubkey 1.2 client sends pubkey to server: getauthchallenge/pubkey 1.3 Server generates a random nonce and stores in a temporary DB (firestore) the (nonce, pubkey) combination, with some TTL (e.g. of 1hr). 1.4 Server returns "nonce" to the client

  3. On the client, take the nonce and have the user sign it with their wallet (e.g. Phantom). Note that the "nonce" actually also contains a user readable message on the lines of "Sign this message with your wallet to sign in to desolate.space. Nonce=28342765"

  4. Send the signed payload back to the server: completeauthchallenge/(pubkey, payload, payload signature) 3.1 Extract nonce from payload 3.2 Lookup pubkey and TTL from DB, based off the nonce 3.3 Verify the payload contains "Sign this message with your wallet to sign in to desolate.space" 3.4 Verify signature corresponds to pubkey and isn't expired 3.5 If it's all correct, generate a JWT session token with Firebase (Firebase Auth Custom Tokens), and send it back to client

  5. Client receives Firebase Auth Custom token 4.1. Call FirebaseAuth#signInWithCustomAuth(): this creates a refresh token (persistent session) which gets stored locally on the browser storage. And from this, access tokens are derived automatically every hour or so 4.2. Use firebase auth libraries for verifying access both on the client as well as on the server side

Links:

alfonso-paella commented 2 years ago

Some useful links:

alfonso-paella commented 2 years ago

Library for crypto: https://github.com/dchest/tweetnacl-js