Last active
June 24, 2019 20:59
-
-
Save EliJDonahue/a4f1270649704c345bb6502c950727ec to your computer and use it in GitHub Desktop.
Get an Innovator OAuth token. For definitions of httpReq() and getJSON(), check out https://github.com/ArasLabs/rest-upload-example/blob/master/Code/js/my-upload.js.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Retrieves a token from the Innovator auth server using the provided user credentials. | |
* Returns a new OAuth token string. | |
* | |
* @param creds An object containing credentials for connecting to an Innovator instance. | |
* @param {string} creds.url Innovator url. Ex: http://localhost/Innovator12 | |
* @param {string} creds.db Innovator database name. | |
* @param {string} creds.user User's login name. | |
* @param {string} creds.pwd MD5 hash of the user's password. | |
* | |
* @returns {string} The OAuth token returned from the server. | |
*/ | |
async function getOAuthToken(creds) { | |
try { | |
// get the OAuth server url | |
var discovery_url = creds.url + "/Server/OAuthServerDiscovery.aspx"; | |
var oauth_res = await httpReq("GET", discovery_url); | |
var oauth_obj = getJSON(oauth_res); | |
var oauth_url = oauth_obj.locations[0].uri; | |
// get the OAuth server token endpoint url | |
var get_endpoint_url = oauth_url + ".well-known/openid-configuration"; | |
var endpoint_res = await httpReq("GET", get_endpoint_url); | |
var endpoint_obj = getJSON(endpoint_res); | |
var token_url = endpoint_obj.token_endpoint; | |
// build the OAuth token request | |
var token_headers = []; | |
var token_body = new FormData(); | |
token_body.set("grant_type", "password"); | |
token_body.set("scope", "Innovator"); | |
token_body.set("client_id", "IOMApp"); | |
token_body.set("username", creds.user); | |
token_body.set("password", creds.pwd); | |
token_body.set("database", creds.db); | |
// get the token | |
var token_res = await httpReq("POST", token_url, token_headers, token_body); | |
var token_obj = getJSON(token_res); | |
var token = token_obj.access_token; | |
return token; | |
} catch (err) { | |
throw new Error("Error in getOAuthToken: " + err.message); | |
} | |
} | |
// For definitions of httpReq() and getJSON(), check out the project: | |
// https://github.com/ArasLabs/rest-upload-example/blob/master/Code/js/my-upload.js. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment