Google OAuth Flow
The authentication process uses Laravel Socialite to integrate with Google’s OAuth 2.0 provider:How It Works
Initial Login Request
When a user clicks the login button, they’re redirected to Google’s OAuth consent screen:stateless() method is used because this is an API-style authentication flow that doesn’t rely on server-side sessions during the OAuth handshake.
OAuth Callback
After the user authenticates with Google, they’re redirected back to the callback URL with an authorization code:Exchange Authorization Code
Laravel Socialite exchanges the authorization code for an access token and retrieves the user’s profile.
Domain Validation
If domain restrictions are configured, the user’s email domain is checked against the allowed domain.
Create or Update User
The user account is created if new, or updated if it exists, using Google profile data.
Complete Callback Implementation
Configuration
Google OAuth Credentials
Set up your Google OAuth application in the Google Cloud Console and configure the credentials:The redirect URI must match exactly what’s configured in your Google Cloud Console OAuth app settings.
Google Cloud Console Setup
Create OAuth Application
In Google Cloud Console, create a new OAuth 2.0 Client ID under APIs & Services > Credentials.
Domain Restriction
One of the key security features is the ability to restrict authentication to specific email domains.Enabling Domain Restrictions
Configure the allowed domain in your environment:@yourcompany.com can authenticate.
How Domain Validation Works
- Extracts the domain from the user’s email address
- Compares it case-insensitively to the configured allowed domain
- Rejects the login if domains don’t match
Multiple Domain Support
Currently, the system supports a single allowed domain. If you need multiple domains, you can modify the validation logic:User Management
User Model
Authenticated users are stored in theusers table with the following attributes:
Create or Update Logic
TheupdateOrCreate method ensures:
- New users are automatically created on first login
- Existing users get their profile updated if changed in Google
Session Management
Login with Remember Me
After successful authentication, users are logged in with a persistent session:remember: true parameter creates a long-lived “remember me” cookie, keeping users logged in across browser sessions.
Session Duration
Laravel’s default session configuration applies:- Session lifetime: 120 minutes (configurable in
config/session.php) - Remember me token: 2 weeks (Laravel default)
Logout
Users can log out, which destroys their session and invalidates tokens:- Logs the user out of Laravel’s authentication
- Invalidates the current session
- Regenerates the CSRF token (prevents reuse)
- Redirects to the home page
Logging out of NetBird Selfservice does not log you out of your Google account. It only terminates the session within this application.
Security Considerations
Stateless OAuth
Using
stateless() mode prevents CSRF attacks during the OAuth flow by not storing state in server sessionsHTTPS Required
Always use HTTPS in production. OAuth redirects with sensitive tokens must be encrypted
Token Regeneration
CSRF tokens are regenerated on logout to prevent session fixation attacks
Domain Whitelisting
Domain restrictions prevent unauthorized access from external Google accounts
User Profile Integration
The user’s Google profile information is stored and can be used throughout the application:Avatar Display
User Attributes
After authentication, you have access to:| Attribute | Source | Description |
|---|---|---|
name | Google Profile | Full name of the user |
email | Google Account | Email address |
google_id | Google User ID | Unique identifier from Google |
avatar | Google Profile Picture | URL to profile image |
Troubleshooting
”Only users with @domain email addresses are allowed”
Cause: Your email domain doesn’t matchNETBIRD_ALLOWED_DOMAIN.
Solution:
- Verify you’re using the correct email domain
- Check the
NETBIRD_ALLOWED_DOMAINconfiguration - Contact your administrator to add your domain
Redirect URI Mismatch
Cause: The callback URL doesn’t match what’s configured in Google Cloud Console. Solution:- Ensure
GOOGLE_REDIRECT_URImatches your Google OAuth app configuration - Check for trailing slashes and http vs https
”Invalid Client” Error
Cause: Incorrect Google OAuth credentials. Solution:- Verify
GOOGLE_CLIENT_IDandGOOGLE_CLIENT_SECRETare correct - Ensure the OAuth app is enabled in Google Cloud Console
- Check that you’re using credentials from the correct Google Cloud project
Best Practices
Enable Domain Restrictions
Always set
NETBIRD_ALLOWED_DOMAIN in production to limit access to your organizationUse HTTPS
Never use OAuth over HTTP in production - Google requires HTTPS for OAuth redirects
Monitor Failed Logins
Track authentication failures to detect unauthorized access attempts
Keep Credentials Secret
Never commit OAuth credentials to version control - use environment variables