Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make a version that uses .well-known/openid-configuration #1

Open
horner opened this issue Oct 8, 2024 · 3 comments · May be fixed by #10
Open

Make a version that uses .well-known/openid-configuration #1

horner opened this issue Oct 8, 2024 · 3 comments · May be fixed by #10
Assignees

Comments

@horner
Copy link
Member

horner commented Oct 8, 2024

Consider a version that uses this:
https://www.npmjs.com/package/openid-client
image

Thanks to the stupid smart gpt: https://chatgpt.com/share/6704fe5f-d350-8004-bd5c-493dda7e30dc

@abroa01 abroa01 self-assigned this Oct 9, 2024
@horner
Copy link
Member Author

horner commented Oct 9, 2024

A non-working proof of concept:

const { Issuer } = require('openid-client');
const http = require('http');
const qs = require('querystring');
const readline = require('readline');

// Read client secret from environment variable
let clientSecret = process.env.CLIENT_SECRET;

if (!clientSecret) {
  const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
  });

  rl.question('Enter the client secret: ', (input) => {
    clientSecret = input;
    rl.close();
    startServer();
  });
} else {
  startServer();
}

async function startServer() {
  try {
    const issuer = await Issuer.discover('https://your-openid-provider/.well-known/openid-configuration');
    console.log('Discovered issuer %s %O', issuer.issuer, issuer.metadata);

    const client = new issuer.Client({
      client_id: 'MIE-localhost',
      client_secret: clientSecret,
      redirect_uris: ['http://localhost:8080/callback'],
      response_types: ['code']
    });

    http.createServer((req, res) => {
      const urlParts = req.url.split('/');
      const path = urlParts[1];
      
      if (path === '') {
        const authURL = client.authorizationUrl({
          scope: 'openid email profile',
          state: 'some_random_string'
        });

        res.writeHead(200, {'Content-Type': 'text/html'});
        res.end(`<a href="${authURL}">Log in with OpenID</a>`);
      } else if (path === 'callback') {
        const query = qs.parse(urlParts[1].split('?')[1]);
        
        client.callbackParams(req).then(params => {
          return client.callback('http://localhost:8080/callback', params, { state: 'some_random_string' });
        }).then(tokenSet => {
          console.log('Received and validated tokens %j', tokenSet);
          res.writeHead(200, {'Content-Type': 'text/plain'});
          res.end(`Access Token: ${tokenSet.access_token}`);
        }).catch(err => {
          console.error('Error handling callback:', err);
          res.writeHead(500, {'Content-Type': 'text/plain'});
          res.end('Authentication failed');
        });

      } else {
        res.writeHead(404, {'Content-Type': 'text/plain'});
        res.end('Not Found');
      }
    }).listen(8080, () => {
      console.log('Server is listening on port 8080');
    });

  } catch (error) {
    console.error('Failed to discover issuer:', error);
  }
}

@abroa01
Copy link
Collaborator

abroa01 commented Nov 25, 2024

I have created a version that uses the openid client's /.well-known/configuration over OAuth 2.0.
Also, created a PR. check out at below link -
PR

@abroa01 abroa01 linked a pull request Nov 25, 2024 that will close this issue
@abroa01
Copy link
Collaborator

abroa01 commented Nov 25, 2024

@horner , Hi Doug,
Can you please merge the PR for this issue, that I created?

check out at below link -
#10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants