UNPKG

react-native-ibm-mobilefirst

Version:

React Native SDK for IBM Mobile Foundation on IBM Cloud

81 lines (67 loc) 2.92 kB
/* Licensed Materials - Property of IBM * 5725-I43 (C) Copyright IBM Corp. 2018. All Rights Reserved. * US Government Users Restricted Rights - Use, duplication or * disclosure restricted by GSA ADP Schedule Contract with IBM Corp. */ /* author - Srihari Kulkarni | skulkarni@in.ibm.com | Slack - @Srihari */ import { NativeModules, } from 'react-native'; var wlAuthManager = NativeModules.RNWLAuthorizationManager; import {ParamTypes, _mandatoryParam, _checkParamType} from './Utils' /** * This class manages the OAuth interaction between the device and the authorization server. */ class WLAuthorizationManager{ /** * @ignore * @constructor */ constructor(){ } /** * Initiates the OAuth flow with the MobileFirst server to get an access token for the specified resource scope. * @param {string} scope - Scope for which the access token is to be returned. * @return {Promise<string>} If success, the access token string for the resource scope. If failed, the error object with additional failure information. */ async obtainAccessToken(scope = _mandatoryParam('scope')){ var token = await wlAuthManager.obtainAccessToken(scope); return token; } /** * Logs in to the specified security check. * @param {string} securityCheck - The security check to log into. * @param {Object} credentials - Credentials for logging in to the security check in JSON format. * @return {Promise<string>} Empty string if success. If failed, the error object with additional failure information. */ async login(securityCheck = _mandatoryParam('securityCheck') , credentials = _mandatoryParam('credentials')){ var result = await wlAuthManager.login(securityCheck, credentials); return result; } /** * Logs out of a specified security check * @param {string} securityCheck - The security check to log out of * @return {Promise<string>} Empty string if success. If failed, the error object with additional failure information. */ async logout(securityCheck =_mandatoryParam('securityCheck') ){ var result = await wlAuthManager.logout(securityCheck); return result; } /** * Clears the provided access token. * Note: When failing to access a resource with an obtained token, call the clearAccessToken method to clear the invalid token before calling WLAuthorizationManager.obtainAccessToken to obtain a new access token. * @param {string} scope - The scope whose access token must be cleared. */ clearAccessToken(scope = _mandatoryParam('scope')){ wlAuthManager.clearAccessToken(scope); } /** * Set Timeout for oauth session * @param {number} timeout - The timeout value for oauth session. */ setLoginTimeout(timeout = _mandatoryParam("timeout")) { _checkParamType(timeout, ParamTypes.NUMBER); wlAuthManager.setLoginTimeout(timeout); } } export default WLAuthorizationManager