This project demonstrates how to authenticate users with MetaMask, Phantom or any other browser wallet that supports Ethereum network. It provides a simple web interface that allows users to connect their MetaMask wallet and display their Ethereum address.
- Click the Connect Wallet button to prompt the MetaMask wallet or some other to connect.
- Once connected, your Ethereum account address will be displayed on the page.
- If there are any errors during the connection, appropriate messages will be shown.
The easiest way to install eth-auth is to download pre-built binaries for your platform. You can find them on the Releases page.
If you want to build the application from source, you can follow these steps:
Prerequisites:
- Go programming language installed
- MetaMask browser extension installed
-
Clone the repository:
git clone https://github.com/fmiskovic/eth-auth.git && cd eth-auth
-
Run the server:
make run
-
Open your browser and navigate to
http://localhost:8080
to see the application and authenticate.
This Go service integrates MetaMask authentication using the Ethereum blockchain to verify users. The service provides two main API endpoints: /nonce
and /auth
. Below is an outline of how the process works:
-
User Connects to MetaMask:
- The front-end prompts the user to connect their MetaMask wallet.
- The user's Ethereum account (wallet address) is retrieved once they approve the connection.
-
Request a Nonce (Server-Side):
- Once connected, the front-end sends a request to the server to obtain a unique nonce via the
/nonce
endpoint. - The server generates a random nonce and associates it with the user’s Ethereum address.
- This nonce is sent back to the client.
- Once connected, the front-end sends a request to the server to obtain a unique nonce via the
-
User Signs the Nonce (Client-Side):
- The front-end uses MetaMask to request that the user sign the nonce with their private key.
- MetaMask provides the signature, which is sent back to the server in an authentication request.
-
Server Verifies Signature:
- The server verifies the signature using the public Ethereum address.
- If the signature matches, the authentication is successful.
- The server can then issue a session token (or similar) to manage user sessions.
- Nonce values are used to prevent replay attacks, ensuring that each authentication attempt is unique.
- It’s important to securely manage session tokens (or other forms of session management) after successful authentication.
.
├── api
│ │── api.go # API endpoints initialization and routing
│ │── error.go # Error handling for the API
│ │── handler.go # Request handlers for the API
│ │── handler_auth.go # Request handlers for the authentication
│ └── handler_nonce.go # Request handlers for the nonce generation
├── logging
│ └── logging.go # Logger used across the application
├── server
│ └── server.go # Server configuration and initialization
├── static
│ ├── style.css # Styles for the web application
│ ├── script.js # JavaScript for MetaMask authentication
│ └── index.html # Main HTML file
├── store
│ └── storer.go # In-Memory Store for nonce values
├── main.go # Main entry point of the application
└── README.md # Project documentation
This project is licensed under the MIT License