Authentication - gbsouzadev/buddget GitHub Wiki
A client must use a valid OAuth token
Application Credentials
A client must have its own set of ID and secret.
Create OAuth Application token
//TODO: add the steps to create Application AUTH.
User Authentication
- Use the OAuth 2 application credentials to create a new account via the Sign up endpoint
- Use the OAuth 2 application credentials to sign in the newly created user via the Sign in endpoint
- The sign in response will include a user token required for all endpoints
Architecture
Here you can see how the Authentication flow was implemented in a high level
Frontend
sequenceDiagram
participant Browser as User's browser
participant Router as React Router
participant React as LoginScreen Component
participant Actions as Redux Actions
participant Reducers as Redux Reducers
participant Store as Redux Store
participant Api as Backend API
Browser->>Router: User goes to /login
Router->>React: Renders LoginScreen Component
React->>Actions: Dispatches a login action on form submission
Actions-->>Reducers: Login request action passed to Reducers
Reducers->>Store: New state (loading state) returned to Store
Store-->>React: Updates the Login Component (shows loading)
Actions->>Api: Sends API request with credentials
Api-->>Actions: Response returned from API
Actions-->>Reducers: Success/Failure action passed to Reducers
Reducers->>Store: New state (success or error state) returned to Store
Store-->>React: Updates the Login Component (Logged in or show error)
React->>Router: Redirects to different route (e.g., /profile)
- A user navigates to the /login page.
- The react-router is responsible for rendering the LoginScreen Component.
- The React LoginScreen Component dispatches a Redux action for login when user submits the form.
- The action changes the state to loading and updates the component showing loading behaviour.
- The same action sends API request with user credentials.
- Receive the response from the Backend API.
- Upon response it dispatches either success or failure action.
- The Reducer changes state according to the result (either user logged in state or error state)
- The Store passes this new state to the LoginScreen Component.
- The Login Component responds to this new state (either logging the user in or showing an error).
- If login is successful, LoginScreen Component redirects the user to a new route on react-router, such as /profile.
Backend
sequenceDiagram
participant User as User's Browser
participant Controller as Spring Controller
participant Service as Authentication Service
participant JwtUtil as JWT Utility
participant Database as User Database
participant Filter as Security Filter
User->>Controller: POST request (login) with credentials
Controller->>Service: Invokes authentication logic
Service->>Database: Validates Credentials
Database-->>Service: User found (or invalid credentials)
opt Credentials Valid
Service->>JwtUtil: Calls to generate JWT
JwtUtil-->>Service: Provides JWT
Service->>Controller: Provides JWT & user details
Controller-->>User: Returns JWT to user
end
opt Credentials Invalid
Service->>Controller: Invalid credentials error
Controller-->>User: Returns error
end
User->>Filter: request to Secure Endpoint with JWT
Filter->>JwtUtil: Validates JWT
opt JWT Valid
JwtUtil-->>Filter: Validates JWT
Filter->>Controller: Passes request to Controller
Controller-->>User: Returns requested data
end
opt JWT Invalid
JwtUtil-->>Filter: JWT invalid error
Filter-->>User: Returns error (Unauthorized access)
end
- The user sends a POST request to the server with his username and password.
- The Spring Controller, upon receiving the request, invokes the Authentication Service to validate the user credentials.
- The Authentication Service checks the credentials against the User Database.
- If credentials are valid:
- The Authentication Service asks the JWT Utility to generate a JWT.
- The JWT Utility returns the newly created JWT.
- The JWT and user details are then sent back to the Controller, which returns the JWT to the user.
- If credentials are invalid, an error message is returned to the Controller, which then returns the error to the user.
- The user sends a request to a Secure Endpoint along with their JWT.
- The Security Filter intercepts this request, validates the JWT using the JWT Utility.
- If JWT is valid:
- This valid JWT is then relayed back to the Security Filter.
- The Security Filter then passes the validated request to the corresponding Spring Controller.
- The Controller then returns the requested data to the User.
- If JWT is invalid:
- An error message is relayed back to the Security Filter.
- The Security Filter returns an error message to the user (usually something along the line of Unauthorized access).