Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Carl Godkin
osdu-tutorials-node
Commits
9c73e7a6
Commit
9c73e7a6
authored
Jan 09, 2020
by
Carl Godkin
Browse files
Added scope for Azure.
Added catch() for unhandled promise.
parent
e912566c
Changes
1
Hide whitespace changes
Inline
Side-by-side
quickstart/auth/routes.js
View file @
9c73e7a6
...
...
@@ -3,36 +3,42 @@ const axios = require('axios');
const
authClient
=
require
(
'
./client-factory
'
);
const
OSDU_TARGET_PLATFORM
=
process
.
env
.
OSDU_TARGET_PLATFORM
;
const
AUTH_SCOPES
=
process
.
env
.
OSDU_AUTH_SCOPES
.
split
(
'
,
'
).
join
(
'
'
);
module
.
exports
.
authEntry
=
async
(
req
,
res
)
=>
{
const
client
=
await
authClient
.
clientFactory
.
getClient
();
res
.
redirect
(
client
.
authorizationUrl
());
res
.
redirect
(
client
.
authorizationUrl
(
{
scope
:
AUTH_SCOPES
}
));
};
module
.
exports
.
authCallback
=
async
(
req
,
res
)
=>
{
try
{
const
client
=
await
authClient
.
clientFactory
.
getClient
();
const
params
=
client
.
callbackParams
(
req
);
const
data
=
await
client
.
callback
(
authClient
.
CALLBACK_URL
,
params
,
{});
let
bearer
=
''
;
if
(
OSDU_TARGET_PLATFORM
==
'
azure
'
)
bearer
=
'
Bearer
'
;
axios
.
defaults
.
headers
.
post
[
'
Authorization
'
]
=
`
${
bearer
}${
data
.
access_token
}
`
;
axios
.
defaults
.
headers
.
post
[
'
X-Id-Token
'
]
=
data
.
id_token
;
// If interested in user info...
client
.
userinfo
(
data
.
access_token
)
// => Promise
.
then
(
function
(
userinfo
)
{
console
.
log
(
'
userinfo %j
'
,
userinfo
);
});
res
.
send
(
`<html><body><pre>
${
JSON
.
stringify
(
data
,
null
,
4
)}
</pre></body></html>`
);
const
client
=
await
authClient
.
clientFactory
.
getClient
();
const
params
=
client
.
callbackParams
(
req
);
const
data
=
await
client
.
callback
(
authClient
.
CALLBACK_URL
,
params
,
{});
let
bearer
=
''
;
if
(
OSDU_TARGET_PLATFORM
==
'
azure
'
)
bearer
=
'
Bearer
'
;
axios
.
defaults
.
headers
.
post
[
'
Authorization
'
]
=
`
${
bearer
}${
data
.
access_token
}
`
;
axios
.
defaults
.
headers
.
post
[
'
X-Id-Token
'
]
=
data
.
id_token
;
// If interested in user info...
client
.
userinfo
(
data
.
access_token
)
// => Promise
.
then
(
function
(
userinfo
)
{
console
.
log
(
'
userinfo %j
'
,
userinfo
);
}).
catch
(
function
(
err
)
{
// Fetching userinfo in this way works on AWS but not on Azure.
// Note sure why. If you know, open an issue or submit PR to fix!
console
.
error
(
'
Unable to get userinfo
'
);
console
.
error
(
err
);
});
res
.
send
(
`<html><body><pre>
${
JSON
.
stringify
(
data
,
null
,
4
)}
</pre></body></html>`
);
}
catch
(
error
)
{
console
.
error
(
error
);
console
.
error
(
error
);
}
};
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment