Skip to content

Latest commit

 

History

History
318 lines (236 loc) · 6.17 KB

File metadata and controls

318 lines (236 loc) · 6.17 KB

✅ PKCE OAuth Implementation Complete!

What Was Implemented

Modern Authorization Code Flow + PKCE - the recommended OAuth 2.1 approach for mobile apps (2026).


Changes Made

1. Added flutter_appauth Package

File: pubspec.yaml

dependencies:
  flutter_appauth: ^12.0.0  # OAuth 2.0 with PKCE support

2. Configured Android for OAuth Redirect

File: android/app/src/main/AndroidManifest.xml

Added redirect activity:

<activity
    android:name="net.openid.appauth.RedirectUriReceiverActivity"
    android:exported="true"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data
            android:scheme="gitdoit"
            android:host="oauth2redirect" />
    </intent-filter>
</activity>

3. Configured iOS for OAuth Redirect

File: ios/Runner/Info.plist

Added custom URL scheme:

<key>CFBundleURLTypes</key>
<array>
  <dict>
    <key>CFBundleTypeRole</key>
    <string>Editor</string>
    <key>CFBundleURLSchemes</key>
    <array>
      <string>gitdoit</string>
    </array>
  </dict>
</array>

4. Created GitHub Auth Service with PKCE

File: lib/services/github_auth_service.dart

Modern authentication service:

  • Uses Authorization Code Flow + PKCE
  • No manual code entry
  • One-tap login via browser
  • Secure token storage
  • Automatic PKCE code verifier generation

5. Updated Onboarding Screen

File: lib/screens/onboarding_screen.dart

Simplified login flow:

Future<void> _loginWithOAuth() async {
  final token = await _githubAuth.signIn();
  // Token received → navigate to dashboard
}

How It Works (PKCE Flow)

1. User clicks "Login with GitHub"
   ↓
2. System browser opens (Chrome/Safari)
   ↓
3. User logs in to GitHub
   ↓
4. User authorizes GitDoIt app
   ↓
5. GitHub redirects to: gitdoit://oauth2redirect?code=AUTH_CODE
   ↓
6. App receives authorization code
   ↓
7. App exchanges code for access token (with PKCE)
   ↓
8. Token stored in FlutterSecureStorage
   ↓
9. User logged in! ✅

Setup Instructions

Step 1: Create GitHub OAuth App

  1. Go to https://github.com/settings/developers
  2. Click "New OAuth App"
  3. Fill in:
    Application name: GitDoIt
    Homepage URL: https://github.com/berlogabob/flutter-github-issues-todo
    Authorization callback URL: gitdoit://oauth2redirect
    
  4. Click "Register application"
  5. Copy Client ID (e.g., Iv1.xxxxxxxxxxxx)

Step 2: Configure .env File

cp .env.default .env
nano .env

Add your Client ID:

GITHUB_CLIENT_ID=Iv1.xxxxxxxxxxxx

Step 3: Run the App

flutter run

Step 4: Test Login

  1. Click "Login with GitHub"
  2. Browser opens GitHub login
  3. Log in and authorize
  4. Redirected back to app
  5. Logged in! ✅

Benefits Over Device Flow

Feature Device Flow (Old) PKCE Flow (New)
User Experience Enter 8-digit code One-tap login
Steps 5+ steps 2 steps
Security Basic PKCE (more secure)
Browser Manual copy/paste Automatic redirect
Recommended ❌ Deprecated ✅ Recommended 2026

Technical Details

PKCE (Proof Key for Code Exchange)

What it does:

  • Prevents authorization code interception attacks
  • Generates random code_verifier per login
  • Creates code_challenge from verifier
  • GitHub validates challenge on token exchange

Why it's better:

  • No client secret needed (safe for mobile apps)
  • Protects against man-in-the-middle attacks
  • Required by OAuth 2.1 for public clients

Redirect URI

Format: gitdoit://oauth2redirect

Why custom scheme:

  • Works on Android and iOS
  • No need for HTTPS domain
  • Instant app launch after authorization

Configuration:

  • Android: AndroidManifest.xml intent-filter
  • iOS: Info.plist CFBundleURLTypes
  • GitHub OAuth App: Authorization callback URL

Troubleshooting

"GITHUB_CLIENT_ID is not configured"

Fix:

nano .env
# Add: GITHUB_CLIENT_ID=Iv1.xxxxxxxxxxxx

Redirect fails on Android

Check:

<!-- AndroidManifest.xml -->
<data android:scheme="gitdoit" android:host="oauth2redirect" />

Make sure scheme is lowercase.

Redirect fails on iOS

Check:

<!-- Info.plist -->
<key>CFBundleURLSchemes</key>
<array>
  <string>gitdoit</string>
</array>

Login cancelled

Possible causes:

  • User closed browser
  • Network error
  • Invalid Client ID

Check logs:

flutter run --verbose 2>&1 | grep "GitHubAuthService"

Security Notes

✅ What's Secure

  • PKCE prevents code interception
  • No client secret in app
  • Tokens in FlutterSecureStorage
  • HTTPS for all GitHub API calls

❌ What to Avoid

  • Don't commit .env to git
  • Don't log tokens
  • Don't use HTTP (only HTTPS)
  • Don't store tokens in plain text

Next Steps

Before Testing

  1. ✅ Create GitHub OAuth App
  2. ✅ Add Client ID to .env
  3. ✅ Configure redirect URI in GitHub
  4. ✅ Run flutter run

After Testing

  1. Test on physical Android device
  2. Test on physical iOS device
  3. Test token refresh (if implemented)
  4. Test logout and re-login

API Reference

GitHubAuthService

// Login
final token = await GitHubAuthService().signIn();

// Logout
await GitHubAuthService().signOut();

// Get token
final token = await GitHubAuthService().getAccessToken();

// Check if logged in
final isLoggedIn = await GitHubAuthService().isLoggedIn();

Summary

What Changed:

  • ✅ Replaced Device Flow with PKCE
  • ✅ Added flutter_appauth package
  • ✅ Configured Android/iOS redirect
  • ✅ Created modern auth service
  • ✅ Simplified login UX

Benefits:

  • ✅ One-tap login (no code entry)
  • ✅ More secure (PKCE)
  • ✅ Better UX (native browser)
  • ✅ Recommended by GitHub

Setup:

  1. Create GitHub OAuth App
  2. Add Client ID to .env
  3. Run app
  4. Login works! ✅

This is the modern, production-ready way to authenticate with GitHub in 2026! 🚀