node.js - Αδυναμία πιστοποίησης στο Google OAuth2.0 για εφαρμογές διακομιστή προς διακομιστή


Προσπαθώ να ρυθμίσω τον έλεγχο ταυτότητας Google OAuth ώστε να μπορέσω να πάρω ένα access token για μια υπηρεσία λογαριασμού και να μπορώ να χρησιμοποιήσω άλλες υπηρεσίες μέσω REST API όπως το Cloud Storage. Το πρόβλημα είναι ότι λαμβάνω σφάλμα 400 όταν κάνω το αίτημα για το access token και το μήνυμα είναι γενικό, οπότε δεν μπορώ να ξέρω τι δεν λειτουργεί. Χρησιμοποιώ Typescript, Node, και μερικά πακέτα όπως jsonwebtokens ή jwt-simple για την υπογραφή του JWT.

Κάποιες ιδέες αν μου ξεφεύγει κάτι;

import moment from 'moment';
import jwt from 'jwt-simple';
import jsonwebtoken from 'jsonwebtoken'; // try wuth this one for signing the JWT too but same result
import axios from 'axios';
import base64Url from 'base64url';

export const authentication = function (): void {
    const nowInUnix = moment().unix();
    const secret = 'MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCc9HWEZ2VtoIt5...........';
    //this's supposed to be my private key so I removed the beginning and end from it 'cause in the file from where i get it was like this: "private_key": "-----BEGIN PRIVATE KEY-----MY_KEY-----END PRIVATE KEY-----\n"

    let header = { "alg": "RS256", "typ": "JWT" };
    const headerEncoded = base64Url.encode(JSON.stringify(header));
    let claims = {
        "iss": "[email ",
        "scope": "https://www.googleapis.com/auth/devstorage.full_control",
        "aud": "https://oauth2.googleapis.com/token",
        "exp": nowInUnix + 2000,
        "iat": nowInUnix
    };
    const claimsEncoded = base64Url.encode(JSON.stringify(claims));
    // console.log(claimsEncoded);
    const stringToEncrypt = headerEncoded + '.' + claimsEncoded;
    // console.log(stringToEncrypt);
    const signature = jwt.encode(stringToEncrypt, secret);
    // console.log(signature);
    const fullToken = headerEncoded + '.' + claimsEncoded + '.' + signature;
    console.log(fullToken);

    // Something else i don't know if how to pass the params, I assumed it's like query params but I also tried sending the in the body request and same result.
    axios.post(`https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${fullToken}`)
        .then(res => console.log('todo ok'))
        .catch(e => console.log(e));
}

Αυτό είναι το σφάλμα που λαμβάνω: BAD_REQUEST: 400 στιγμιότυπο οθόνης του σφάλματος που βλέπω

Διάβασα τα Google Docs: https://developers.google.com/identity/protocols/oauth2/service-account https://cloud.google.com/storage/docs/uploading-objects#rest-upload-objects

Απάντηση #1

Διόρθωσα τον κώδικα βάσει του πρώτου σχολίου, και τώρα λειτουργεί έτσι:

import moment from 'moment';
import jsonwebtoken from 'jsonwebtoken';
import axios from 'axios';

export const authentication = function (): void {
    const nowInUnix = moment().unix();
    const secret = "-----BEGIN PRIVATE KEY-----YOUR_KEY-----END PRIVATE KEY-----\n";
    let claims = {
        "iss": "[email ",
        "scope": "https://www.googleapis.com/auth/devstorage.full_control",
        "aud": "https://oauth2.googleapis.com/token",
        "exp": nowInUnix + 3600,
        "iat": nowInUnix
    };
    const token = jsonwebtoken.sign(claims, secret, { algorithm: 'RS256'});
    const response: any = await axios.post(`https://oauth2.googleapis.com/token?grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion=${token}`)
        .catch(e => console.log('Error occured: \n' + e));
    console.log(response.data);
}

απάντηση :

{
  access_token: 'MY_TOKEN',
  expires_in: 3599,
  token_type: 'Bearer'
}