College-Project-DAVV / IET-DAVV-FACULTY-SECTION-CLIENT

0 stars 0 forks source link

LDAP and OAuth2.0 #2

Open HarshhFarkiya opened 10 months ago

HarshhFarkiya commented 10 months ago

const express = require('express'); const bodyParser = require('body-parser'); const { google } = require('googleapis'); const ldap = require('ldapjs');

const app = express(); const port = 3001;

app.use(bodyParser.json());

// Your LDAP configuration const ldapClient = ldap.createClient({ url: 'ldap://your-ldap-server', });

// Your Google API credentials const CLIENT_ID = 'your-client-id'; const CLIENT_SECRET = 'your-client-secret'; const REDIRECT_URI = 'http://localhost:3001/oauth2callback';

const oAuth2Client = new google.auth.OAuth2( CLIENT_ID, CLIENT_SECRET, REDIRECT_URI );

app.post('/login', async (req, res) => { const { username, password } = req.body;

try { // LDAP Authentication await new Promise((resolve, reject) => { ldapClient.bind(username, password, (err) => { if (err) { reject(err); } else { resolve(); } }); });

// OAuth 2.0 Flow
const authUrl = oAuth2Client.generateAuthUrl({
  access_type: 'offline',
  scope: ['https://www.googleapis.com/auth/admin.directory.user'],
});

res.json({ authenticated: true, authUrl });

} catch (error) { console.error('Error during authentication:', error); res.json({ authenticated: false }); } });

app.get('/oauth2callback', async (req, res) => { const { code } = req.query;

try { const { tokens } = await oAuth2Client.getToken(code); oAuth2Client.setCredentials(tokens);

// tokens.refresh_token can be stored for future use
res.json({ authenticated: true, accessToken: tokens.access_token });

} catch (error) { console.error('Error during OAuth 2.0 callback:', error); res.json({ authenticated: false }); } });

app.listen(port, () => { console.log(Server is running on port ${port}); });

HarshhFarkiya commented 10 months ago

OAUTH in Backend ----->>>>> const express = require('express'); const axios = require('axios');

const app = express(); const PORT = 3000;

const CLIENT_ID = 'your_client_id'; const CLIENT_SECRET = 'your_client_secret'; const REDIRECT_URI = 'http://your-backend/redirect';

app.get('/login', (req, res) => { // Redirect the user to the OAuth provider's authorization endpoint const authorizationUrl = https://oauth-provider.com/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=email; res.redirect(authorizationUrl); });

app.get('/redirect', async (req, res) => { // Handle the callback from the OAuth provider with the authorization code const code = req.query.code;

// Exchange the authorization code for an access token const tokenResponse = await axios.post('https://oauth-provider.com/token', { client_id: CLIENT_ID, client_secret: CLIENT_SECRET, code, grant_type: 'authorization_code', redirect_uri: REDIRECT_URI, });

// The backend now has the access token, which can be used to make API requests on behalf of the user

res.send('Login successful!'); });

app.listen(PORT, () => { console.log(Server is running on port ${PORT}); });