nodejs call openstack api keystone login authentication (two): Get token

 Gets header inside the token

var http = require('http');
var url = require('url');
var os = require('os');
var fs = require('fs');
var querystring = require('querystring');

var x_subject_token = '';
function keystone_auth() {
    var auth_body = {
        auth: {
            identity: {
                methods: [
                    "password"
                ],
                password: {
                    user: {
                        name: "admin",
                        domain: {
                            name: "default"
                        },
                        password: "sunya.123"
                    }
                }
            }
        }
    };

    var auth_body_string = JSON.stringify(auth_body);

    var options = {
        host: 'xx.xx.xx.xx',
        port: 5000,
        path: '/v3/auth/tokens',
        method: 'POST',
        headers: {
            'Content-Type': 'application/json',
            'Content-Length': auth_body_string.length
        }
    };

    var req = http.request(options, function (res) {
        res.setEncoding('utf8');
        console.log("res:", res);
        console.log("res.headers:", res.headers);
        console.log("res.headers.x-subject-token:", res.headers['x-subject-token']);
        for (var key in res.headers) {
            if (key === 'x-subject-token'){
                console.log(key);
                console.log(res.headers['x-subject-token']);
                x_subject_token = res.headers['x-subject-token']
            }
        }
        res.on('data', function (data) {
            console.log("data:", data);   //打印结果
            console.log(x_subject_token);
        });
    });

    req.write(auth_body_string);

    req.end;
}

keystone_auth();

The key header data returned (we need to extract from the inside out token):

res.headers: {
  date: 'Sat, 28 Dec 2019 17:54:34 GMT',
  server: 'Apache/2.4.6 (CentOS) mod_wsgi/3.4 Python/2.7.5',
  'x-subject-token': 'gAAAAABeB5baGZZJUEXSgxWGiF1xanS6MPEhd5v5QDMRD8tdEEE3ma5AJcq08otdLZRDNEtGFpdk5cbeSZ38ufPm-DQEWDdC__7sEBYft_GQrpq6gTlUQNk1Sgu8Bo2l8wFeZw-6pBdodKUNL8lJf
6JEzIKi_1Qy_A',
  vary: 'X-Auth-Token',
  'x-openstack-request-id': 'req-3df5a3f2-d762-444b-846a-d088404fd5dc',
  'content-length': '312',
  connection: 'close',
  'content-type': 'application/json'
}

 

Published 127 original articles · won praise 35 · Views 100,000 +

Guess you like

Origin blog.csdn.net/shunzi2016/article/details/103750674