Oauth2 flow without redirect_uri

后端 未结 5 1497
北海茫月
北海茫月 2021-01-05 06:15

I am creating an Android/iOS app which communicates with a Node.js server and would like to identify them securely on my server using Google (and/or Facebook) and OAuth2. I\

相关标签:
5条回答
  • 2021-01-05 06:55

    TL;DR How do you use OAuth2 to authenticate users between my client and my server without redirection?

    You can't. OAuth requires that the user is directed to an authorization (and possibly login) screen, and then redirected back to your app.

    0 讨论(0)
  • 2021-01-05 06:56

    The redirect_uri can be a URL with a custom URL scheme for which the client registered a handler. This is described here: What's a redirect URI? how does it apply to iOS app for OAuth2.0?. It is not so much about "redirecting" it is about a callback endpoint to your app.

    0 讨论(0)
  • 2021-01-05 06:58

    And it become really easy if you use VueJS with https://github.com/guruahn/vue-google-oauth2

    Client side

    import GAuth from 'vue-google-oauth2'
    
    Vue.use(GAuth, {
        clientId: 'xxxxxxx.apps.googleusercontent.com',
        scope: 'profile',
    })
    
    async signWithGoogle() {
        const code = await this.$gAuth.getAuthCode() //
        console.log(code ) // { code: 'x/xxxxxxxxxx' }
        // send the code to your auth server
        // and retrieve a JWT or something to keep in localstorage
        // to send on every request and compare with database
    }
    

    Server side

    import { google } from 'googleapis'
    
    const oauth2Client = new google.auth.OAuth2(GOOGLE_ID, GOOGLE_SECRET, 'postmessage')
    
    google.options({ auth: oauth2Client })
    
    async function getAccount(code) {
        // the code you sent with the client
        const { tokens } = await oauth2Client.getToken(code)
        oauth2Client.setCredentials(tokens)
        const oauth2 = google.oauth2({ version: 'v2' })
        const { data: { id } } = await oauth2.userinfo.get()
        // there you have the id of the user to store it in the database
        // and send it back in a JWT
    }
    
    0 讨论(0)
  • 2021-01-05 06:59

    I had this problem and it took me ages to find the "postmessage" solution that Nepoxx mentions in the comments of the accepted answer here.

    For clarification, here's what worked for me.

    1. Follow steps 1-6 here: https://developers.google.com/identity/sign-in/web/server-side-flow
    2. Install googleapis library npm install --save googleapis
    3. For the server-side token exchange do this:
        var googleapis = require('googleapis');
        var OAuth2 = googleapis.auth.OAuth2;
    
        var oauth2Client = new OAuth2(
           GOOGLE_SSO_CLIENT_ID,
           GOOGLE_SSO_CLIENT_SECRET,
           'postmessage' // this is where you might otherwise specifiy a redirect_uri
        );
    
        oauth2Client.getToken(CODE_FROM_STEP_5_OF_INSTRUCTIONS, function(err, tokens) {
           // Now tokens contains an access_token and an optional refresh_token. Save them.
        });
    
    0 讨论(0)
  • 2021-01-05 07:08

    Have you looked at this documentation? https://developers.google.com/accounts/docs/OAuth2InstalledApp#choosingredirecturi

    Choosing a redirect URI

    When you create a client ID in the Google Developers Console, two redirect_uris are created for you: urn:ietf:wg:oauth:2.0:oob and http://localhost. The value your application uses determines how the authorization code is returned to your application.

    http://localhost

    This value signals to the Google Authorization Server that the authorization code should be returned as a query string parameter to the web server on the client. You may specify a port number without changing the Google Developers Console configuration. To receive the authorization code using this URL, your application must be listening on the local web server. This is possible on many, but not all, platforms. If your platform supports it, this is the recommended mechanism for obtaining the authorization code.

    0 讨论(0)
提交回复
热议问题