This project is an Azure Functions application that implements JWT (JSON Web Token) authentication, built with Node.js TypeScript.
- Node.js > 18
- Clone the repository
- Navigate to the project directory
- Install the dependencies:
npm install
- All the client ID & client secret are stored in environment variables. For local testing, check local.settings.json. To add new client ID, just add new entries in the environment variables, the key would be the client ID, and the value would be client secret.
- JWT secret key is stored in environment variables as
jwt_secret_key - JWT expired time is stored in environment variables as
jwt_expire_time
To start the application, execute:
npm start
- Authentication Endpoint: [POST]
/api/auth?client_id=${cliendID}&client_secret=${clientSecret}the response for correct client ID and client secret:curl --location --request POST 'http://localhost:7071/api/auth?client_id=test_client&client_secret=XX0VmfQAk0awWwoBEQSi'the response if either client ID or client secret is wrong or missing:{ "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZGlwdXRlcmEiLCJzdWIiOiJ0ZXN0X2NsaWVudCIsImlhdCI6MTc0MDU1NjMyOCwiZXhwIjoxNzQwNTU3MjI4fQ.d8HcWvSL9yV38rNTFbREmnQDn9phY-jwhrbN-3yQavg", "token_type": "Bearer", "expiresIn": 890 }{ "error": "Invalid credential" } - Protected Endpoint: [POST]
/api/endpointthe response if authenticated successfully:curl --location --request POST 'http://localhost:7071/api/endpoint' \ --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhZGlwdXRlcmEiLCJzdWIiOiJ0ZXN0X2NsaWVudCIsImlhdCI6MTc0MDU1NjMyOCwiZXhwIjoxNzQwNTU3MjI4fQ.d8HcWvSL9yV38rNTFbREmnQDn9phY-jwhrbN-3yQavg'
the response if failed authenticated:{ "message": "You have access to this endpoint" }{ "error": "Unauthorized" }
Contributions are welcome! Please submit a pull request or open an issue to discuss any changes.