Authorization
Encapsulates all of the logic for communication with the Authorization Service in the NPR One API.
Note that consumers should not be accessing this class directly but should instead use the provided pass-through functions in the main NprOneSDK class.
Example:
const nprOneSDK = new NprOneSDK();
nprOneSDK.config = { ... };
const scopes = ['identity.readonly', 'identity.write', 'listening.readonly', 'listening.write'];
nprOneSDK.getDeviceCode(scopes)
.then((deviceCodeModel) => {
// display code to user on the screen
nprOneSDK.pollDeviceCode()
.then(() => {
nprOneSDK.getRecommendation();
});
})
.catch(() => {
nprOneSDK.getDeviceCode(scopes).then(...); // repeat ad infinitum until `pollDeviceCode()` resolves successfully
// In actual use, it may be preferable to refactor this into a recursive function
));
Test:
Static Method Summary
Static Public Methods | ||
public static |
refreshExistingAccessToken(numRetries: number): Promise<AccessToken> Attempts to swap the existing access token for a new one using the refresh token endpoint in the OAuth proxy |
Constructor Summary
Public Constructor | ||
public |
Initializes the controller class with private variables needed later on. |
Method Summary
Public Methods | ||
public |
getDeviceCode(scopes: Array<string>): Promise<DeviceCode> Uses the OAuth proxy to start a |
|
public |
Logs out the user, revoking their access token from the authorization server and removing the refresh token from the secure storage in the backend proxy (if a backend proxy is configured). |
|
public |
Uses the OAuth proxy to poll the access token endpoint as part of a |
Static Public Methods
public static refreshExistingAccessToken(numRetries: number): Promise<AccessToken> source
Attempts to swap the existing access token for a new one using the refresh token endpoint in the OAuth proxy
Params:
Name | Type | Attribute | Description |
numRetries | number |
|
The number of times this function has been tried. Will retry up to 3 times. |
Throw:
if an OAuth proxy is not configured or no access token is set |
Public Constructors
Public Methods
public getDeviceCode(scopes: Array<string>): Promise<DeviceCode> source
Uses the OAuth proxy to start a device_code
grant flow. This function just makes an API call that produces a
device code/user code pair, and should be followed up with a call to pollDeviceCode in order to complete
the process.
Note that device code/user code pairs do expire after a set time, so the consuming client may need to call these 2 functions multiple times before the user logs in. It is a good idea to encapsulate them in a function which can be called recursively on errors; see the example below for details.
Throw:
if an OAuth proxy is not configured |
Example:
function logInViaDeviceCode(scopes) {
nprOneSDK.getDeviceCode(scopes)
.then((deviceCodeModel) => {
displayCodeToUser(deviceCodeModel); // display code to user on the screen
nprOneSDK.pollDeviceCode()
.then(() => {
startPlayingAudio(); // you're now ready to call `nprOneSDK.getRecommendation()` elsewhere in your app
}).catch(logInViaDeviceCode.bind(this, scopes)); // recursively call this function until the user logs in
});
}
public logout(): Promise source
Logs out the user, revoking their access token from the authorization server and removing the refresh token from the secure storage in the backend proxy (if a backend proxy is configured). Note that the consuming client is still responsible for removing the access token anywhere else it might be stored outside of this SDK (e.g. in localStorage or elsewhere in application memory).
Throw:
if an OAuth proxy is not configured or no access token is currently set |
Test:
public pollDeviceCode(): Promise<AccessToken> source
Uses the OAuth proxy to poll the access token endpoint as part of a device_code
grant flow. This endpoint will
continue to poll until the user successfully logs in, or the user goes to log in but then denies the request
for access to their account by this client, or the device code/user code pair expires, whichever comes first.
In the first case, it will automatically set NPROneSDK.accessToken to the newly-generated access token,
and the consuming client can proceed to play recommendations immediately; in the other 2 cases, it will return
a Promise that rejects with a debugging message, but the next course of action would generally be to call
getDeviceCode again and start the whole process from the top.
Throw:
if an OAuth proxy is not configured or |
Example:
function logInViaDeviceCode(scopes) {
nprOneSDK.getDeviceCode(scopes)
.then((deviceCodeModel) => {
displayCodeToUser(deviceCodeModel); // display code to user on the screen
nprOneSDK.pollDeviceCode()
.then(() => {
startPlayingAudio(); // you're now ready to call `nprOneSDK.getRecommendation()` elsewhere in your app
}).catch(logInViaDeviceCode.bind(this, scopes)); // recursively call this function until the user logs in
});
}