OAuth2.0 Inbound authentication from AWS lambda using password grant_type

shilpamurthy
ServiceNow Employee
ServiceNow Employee

Hello All,

I'm trying to integrate AWS IoT button to create a record on my dev instance every time it is clicked.I am using the basic authentication and it works. I want to use OAuth2.0 to authenticate the inbound REST call to a scripted rest API. I've created an endpoint(OAuth client) on my instance and also a user having web service access whose credentials work when using basic Auth.

When I send the /oauth_token.do from POSTMAN, it gives me the access and refresh tokens as expected, but when accessing the same from lambda, the response given is 302.

This is my AWS lambda function written in Node.js attached below. I must be missing something very basic. Any help would be greatly appreciated!

'use strict';

exports.handler = (event, context, callback) => {

var credentials = {

client: {

    id: '15fd9cxxxxxx',

    secret: '*ti+;oxxxxx'

},

auth: {

    tokenHost: 'http://xxxxx.service-now.com',

    tokenPath: '/oauth_token.do'

}

};

var oauth2 = require('simple-oauth2').create(credentials);

// Get the access token object.

var tokenConfig = {

username: 'xxxxxxxx ',

password: 'yyyyyy'

};

oauth2.ownerPassword.getToken(tokenConfig, (error, result) => {

if (error) {

    console.log('Access Token Error', error.message);

}

var token = oauth2.accessToken.create(result);

console.log("token : ",result);

}

part of the client request from logs   which has all the necessary information:

host: 'surxxx.service-now.com',

            body: 'username=xxxxx&password=yyyyyy&grant_type=password&client_id=15fd94xxxxxxx&client_secret=%2Ati%2B%3Boyyyyy',

            path: '/oauth_token.do',

            httpModule: [Object],

            agentClass: [Object],

            agent: [Object],

            _started: true,

            href: 'http://xxxxxx.service-now.com/oauth_token.do',

            req: [Object],

            ntick: true,

            response: [Circular],

            originalHost: 'surxxx.service-now.com',

find_real_file.png

find_real_file.png

2 REPLIES 2

Chandu Telu
Tera Guru
Tera Guru

phoenix516
Tera Contributor

Hello Silpamurthy,

I was curious if you managed to solve the OAuth2 issue you had.

 

I was also curious if you could share your code for the Basic Auth you have working. Was this on nodeJS? I have a similar call being built from AWS using https and when i place the header of "Authorization" : "Basic <stuff>" i still get an error of:

{"error":{"detail":"Required to provide Auth information","message":"User Not Authenticated"},"status":"failure"}

 

curious what i may be missing...

"use strict";

const https = require("https"); //Make the call using https library

/**
 * Pass the data to send as `event.data`, and the request options as
 * `event.options`. For more information see the HTTPS module documentation
 * at https://nodejs.org/api/https.html.
 *
 * Will succeed with the response body.
 */

exports.handler = (event, context, callback) => {
    
    const options = {
        "host" : "mylab.service-now.com",
        "path" : "/api/9136/endpoint",
        "method" : "POST",
        "headders" : {
            "Content-Type" : "application/json",
            "Authorization" : "Basic <stuff>"
        }
    };
    callback = function(response){
        var str = "";
        
        response.on("data", function(chunk){
            str += chunk;
        });
        response.on("end", function(){
            console.log("end: " + str);
        });
    };
    
    var body = JSON.stringify(event);
    process.stdout.write(body);
    
    https.request(options, callback).end(body); // Make the call
};