package main
import (
"context"
"fmt"
"io"
"log"
"net/http"
"golang.org/x/oauth2"
)
var (
oauth2Config = oauth2.Config{
ClientID: "YOUR_CLIENT_ID",
ClientSecret: "YOUR_CLIENT_SECRET",
RedirectURL: "http://SERVICE_IP_ADDRESS:8080/callback",
Scopes: []string{"open_id", "phone", "email", "profile"},
Endpoint: oauth2.Endpoint{
AuthURL: "https://todu.tech/auth",
TokenURL: "https://todu.tech/oauth2/token",
},
}
oauth2StateString = "random" // A state string to protect against CSRF attacks
)
func main() {
http.HandleFunc("/login", handleLogin)
http.HandleFunc("/callback", handleCallback)
log.Println("Starting server on :8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func handleLogin(w http.ResponseWriter, r *http.Request) {
authURL := oauth2Config.AuthCodeURL(oauth2StateString, oauth2.AccessTypeOffline)
http.Redirect(w, r, authURL, http.StatusFound)
}
func handleCallback(w http.ResponseWriter, r *http.Request) {
state := r.URL.Query().Get("state")
if state != oauth2StateString {
http.Error(w, "Invalid state", http.StatusBadRequest)
return
}
code := r.URL.Query().Get("code")
token, err := oauth2Config.Exchange(context.Background(), code)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to exchange token: %s", err), http.StatusInternalServerError)
return
}
client := oauth2Config.Client(context.Background(), token)
resp, err := client.Get("https://todu.tech/oauth2/userinfo")
if err != nil {
http.Error(w, fmt.Sprintf("Failed to get user info: %s", err), http.StatusInternalServerError)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to read user info: %s", err), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "User info: %s", body)
}
import os
import json
from flask import Flask, redirect, request, url_for
from requests_oauthlib import OAuth2Session
from oauthlib.oauth2 import WebApplicationClient
# OAuth2 configuration - replace with your actual credentials
CLIENT_ID = 'YOUR_CLIENT_ID'
CLIENT_SECRET = 'YOUR_CLIENT_SECRET'
REDIRECT_URI = 'http://SERVICE_IP_ADDRESS:8080/callback'
AUTHORIZATION_BASE_URL = 'https://todu.tech/auth'
TOKEN_URL = 'https://todu.tech/oauth2/token'
USERINFO_URL = 'https://todu.tech/oauth2/userinfo'
# Set up OAuth2 session and client
client = WebApplicationClient(CLIENT_ID)
oauth2_session = OAuth2Session(CLIENT_ID, redirect_uri=REDIRECT_URI)
# Create the Flask app
app = Flask(__name__)
@app.route('/')
def index():
"""Homepage that provides the login link."""
return redirect(url_for('login'))
@app.route('/login')
def login():
"""Redirect to the OAuth2 provider's login page."""
authorization_url, state = oauth2_session.authorization_url(AUTHORIZATION_BASE_URL, access_type="offline")
return redirect(authorization_url)
@app.route('/callback')
def callback():
"""Callback endpoint after OAuth2 provider redirects."""
# Get the authorization code from the callback URL
authorization_response = request.url
# Fetch the access token using the authorization code
token = oauth2_session.fetch_token(TOKEN_URL, authorization_response=authorization_response, client_secret=CLIENT_SECRET)
# Use the access token to get user info
user_info = get_user_info(token)
# Display user info (you can replace this with any logic you want)
return json.dumps(user_info, indent=2)
def get_user_info(token):
"""Fetch user info from the OAuth2 provider."""
oauth2_session.token = token
response = oauth2_session.get(USERINFO_URL)
return response.json()
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)
const express = require('express');
const axios = require('axios');
const simpleOauth2 = require('simple-oauth2');
// OAuth2 configuration - replace with your actual credentials
const CLIENT_ID = 'YOUR_CLIENT_ID';
const CLIENT_SECRET = 'YOUR_CLIENT_SECRET';
const REDIRECT_URI = 'http://SERVICE_IP_ADDRESS:8080/callback';
const USERINFO_URL = 'https://todu.tech/oauth2/userinfo';
// Create an OAuth2 client
const oauth2 = simpleOauth2.create({
client: {
id: CLIENT_ID,
secret: CLIENT_SECRET,
},
auth: {
tokenHost: 'https://todu.tech',
authorizePath: '/auth', // Path to the OAuth2 authorization endpoint
tokenPath: '/oauth2/token', // Path to the token exchange endpoint
},
});
// Create the Express app
const app = express();
// Route to start the OAuth2 flow
app.get('/login', (req, res) => {
const authorizationUri = oauth2.authorizationCode.authorizeURL({
redirect_uri: REDIRECT_URI,
scope: 'openid profile', // Example scope, adjust as needed
state: Math.random().toString(36).substring(2), // Random state value
});
res.redirect(authorizationUri);
});
// Callback route to handle the OAuth2 provider's response
app.get('/callback', async (req, res) => {
const { code } = req.query;
try {
// Get the access token using the authorization code
const tokenParams = {
code: code,
redirect_uri: REDIRECT_URI,
};
// Exchange the code for an access token
const accessToken = await oauth2.authorizationCode.getToken(tokenParams);
const result = oauth2.accessToken.create(accessToken);
// Use the access token to fetch user info
const userInfo = await getUserInfo(result.token.access_token);
// Send back the user info
res.json(userInfo);
} catch (error) {
console.error('Error during the callback', error);
res.status(500).json({ error: 'Internal Server Error' });
}
});
// Helper function to get user information
async function getUserInfo(accessToken) {
try {
const response = await axios.get(USERINFO_URL, {
headers: {
Authorization: `Bearer ${accessToken}`,
},
});
return response.data;
} catch (error) {
console.error('Error fetching user info', error);
throw error;
}
}
// Start the server
const PORT = 8080;
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});