ExpressJS Node.js Angular Ошибка: getaddrinfo ENOTFOUND при получении авторизационного токена OAuth2 API - PullRequest
0 голосов
/ 28 марта 2020

Я написал приложение angular, которое по нажатию кнопки отправляет HTTP-запрос на отправку конечной точке Node / Express. js для токена авторизации.

В рамках работы почтальона я сделал следующее успешно:

  1. Настроил соответствующие учетные данные для токена авторизации OASAUT2 basi c и получения токена доступа
  2. Я вызываю конечную точку отдыха с токеном доступа и создаю пользователь в каталоге ldap.

В приложении angular я создал следующие ключевые компоненты:

  1. Пользовательский интерфейс
  2. Интерфейс AuthResponse
  3. Служба аутентификации
  4. Компонент пользовательского интерфейса

Ниже приведены выдержки из logi c для службы аутентификации в приложении angular:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { User } from  './../auth/user';
import { AuthResponse } from  './../auth/auth-response';
import { tap } from  'rxjs/operators';
import { Observable, BehaviorSubject } from  'rxjs';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

  AUTH_SERVER = "http://localhost:3000";
  authSubject  =  new  BehaviorSubject(false);

  constructor(private httpClient: HttpClient) { }

  register(user: User): Observable<AuthResponse> {
      return this.httpClient.post<AuthResponse>(`${this.AUTH_SERVER}/register`, user).pipe(
        tap((res:  AuthResponse ) => {

          if (res.token) {
            console.log("ACCESS_TOKEN : "+ res.token.access_token);
            localStorage.set("ACCESS_TOKEN", res.token.access_token);
            localStorage.set("EXPIRES_IN", res.token.token_type);
            localStorage.set("ACCESS_TOKEN", res.token.expires_in);
            localStorage.set("EXPIRES_IN", res.token.refresh_token);
            this.authSubject.next(true);
          }
        })

      );
    }
}

Ниже приведен код в angular компоненте просмотра

  register() {
    console.log('fetching registration token');
    this.authService.register(this.user).subscribe((res) => {
      //this.router.navigateByUrl('home');
    });
  }

Ниже приведен фрагмент html, вызов которого содержит кнопку, вызывающую конечную точку регистра:

<div>
      <button (click)="register()" >Register</button> 
</div>

Ниже мое expressjs приложение. js

const express = require('express')
const http = require('https')
const app = express()
const  router  =  express.Router();
const cors = require('cors');
const bodyParser  = require("body-parser")
const api_helper = require('./util/api_helper')

const port = 3000

app.use(cors());
router.use(bodyParser.urlencoded({ extended:  false }));
router.use(bodyParser.json());

router.get('/', (req, res) => {
    res.status(200).send('Welcome to Make REST API Calls to Authorisation Server In Express!');
});

router.get('/getAPIResponse', (req, res) => {
    api_helper.make_API_call('https://jsonplaceholder.typicode.com/todos/1')
    .then(response => {
        res.json(response)
    })
    .catch(error => {
        res.send(error)
    })
})


router.post('/register', (req, res) => {
  console.log('Entering the server endpoint ');

  //following variables are being printed to my browser console successfully
  console.log('angular sent user name = '+req.body.username);
  console.log('angular sent password = '+req.body.password);

  var client_id = 'xxxx';
  var client_secret = 'yyyyyy';
  var auth_header = 'Basic ' + Buffer.from(client_id + ':' + client_secret).toString('base64');

  const data = "grant_type=password&username=joe&password=joepass&client_id=xxxx&client_secret=yyyyyy";

  var authURI = "https://[hostname]:[port-no]/osp/a/idm/auth/oauth2/token";

  const options = {
      hostname: authURI,
      form: data,
      port: [port-no],
      method: 'POST',
      headers: {
          'Content-Type': 'application/x-www-formurlencoded'
          //'Authorization': auth_header
      }
  };

  http.request(options, (res) => {
  //  const req = http.request(options, (res) => {
    console.log(`statusCode: ${res.statusCode}`)
    res.setHeader( 'Access-Control-Allow-Headers', 'Accept,Accept-Language,Content-Language,Content-Type')
    res.on('data', (chunk) => {
      console.log(`BODY: ${chunk}`);
    });
    res.on('end', () => {
      console.log('No more data in response.');
    });
  });

  req.on('error', (error) => {
    console.log('error is ' + error);
  });

});

app.use(router);
app.listen(port, () => console.log(`Node server listening on port ${port}!`))

Следующая конечная точка в моем nodejs приложении. js работает:

router.get ('/ getTestResponse', (req, res) => {api_helper.make_API_call ('https://jsonplaceholder.typicode.com/todos/1 ') .then (response => {res. json (response)}) .catch (error => {res.send (error)})})

Он использует следующую вспомогательную функцию:

const request = require('request')

module.exports = {
    /*
    ** This method returns a promise
    ** which gets resolved or rejected based
    ** on the result from the API
    */
    make_API_call : function(url){
        return new Promise((resolve, reject) => {
            request(url, { json: true }, (err, res, body) => {
              if (err) reject(err)
              resolve(body)
            });
        })
    }
}

Проблема в том, что я получаю следующую ошибку, когда нажимаю кнопку регистрации в приложении angular. Но запускается конечная точка nodejs / register, то есть она регистрирует имя пользователя и пароль, полученные из объекта запроса, на консоль:

Entering the server endpoint 
events.js:288
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND https://[hostname]:[port-no]/osp/a/idm/auth/oauth2/token
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26)
Emitted 'error' event on ClientRequest instance at:
    at TLSSocket.socketErrorListener (_http_client.js:426:9)
    at TLSSocket.emit (events.js:311:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  errno: 'ENOTFOUND',
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: 'https://[hostname]:[port-no]/osp/a/idm/auth/oauth2/token'
}
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! registration-express-app@0.0.1 start: `node ./src/app/app.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the registration-express-app@0.0.1 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/admin/.npm/_logs/2020-03-28T08_56_14_947Z-debug.log

1 Ответ

0 голосов
/ 28 марта 2020

Проблема была в параметрах http.request. Обычно имя хоста: должно быть просто именем хоста, а не https: // [имя хоста]: [номер порта] / xxx / auth / oauth2 / token ! Затем используйте атрибут пути для оставшейся части URI конечной точки, т. Е. / Xxx / auth / oauth2 / token.

Это прогрессировало, но теперь я получаю еще одну ошибку «Ошибка: самозаверяющий сертификат в цепочке сертификатов» I Я закрою этот вопрос на этой заметке, чтобы другие могли извлечь пользу из моего опыта. Возвращение к сообществу.

См. Несколько хороших ссылок ниже:

https://node.readthedocs.io/en/latest/api/https/

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...