public async Task <IActionResult> Post([FromBody] CodeDTO value)
        {
            HttpResponseMessage httpResponseMessage = await _employeeService.GetEmployeeAccessTokenFromApi(value.code);

            if (httpResponseMessage.StatusCode == System.Net.HttpStatusCode.OK)
            {
                OAuthTokenDTO   oAuthTokenDTO = JsonConvert.DeserializeObject <OAuthTokenDTO>(httpResponseMessage.Content.ReadAsStringAsync().Result);
                EmployeeDetails employeeObj   = _employeeService.GetByOrcid(oAuthTokenDTO.orcid);
                if (employeeObj == null)
                {
                    EmployeeDetails employeeDetails = new EmployeeDetails()
                    {
                        AccessToken = oAuthTokenDTO.name, Name = oAuthTokenDTO.orcid, OrcId = oAuthTokenDTO.orcid, Password = "******"
                    };
                    return(CreatedAtAction("accesstoken", new { orcid = employeeDetails.OrcId, password = employeeDetails.Password }));
                }
                else
                {
                    employeeObj.AccessToken = oAuthTokenDTO.access_token;
                    _employeeService.UpdateAccessToken(employeeObj);
                    return(Content(JsonConvert.SerializeObject(employeeObj.AccessToken)));
                }
            }
            else
            {
                //return Content(httpResponseMessage.Content.ReadAsStringAsync().Result);
                return(BadRequest(httpResponseMessage.Content.ReadAsStringAsync().Result));
            }
        }
Beispiel #2
0
        public async Task Test_IsolateTokensBetweenEnvironments()
        {
            MangoPayApi api = new MangoPayApi();

            api.Config.ClientId       = "sdk-unit-tests";
            api.Config.ClientPassword = "******";
            api.Config.BaseUrl        = "https://api.sandbox.mangopay.com";

            OAuthTokenDTO token1 = await api.OAuthTokenManager.GetToken();

            api.Config.ClientId       = "sdk_example";
            api.Config.ClientPassword = "******";
            api.Config.BaseUrl        = "https://api.sandbox.mangopay.com";

            OAuthTokenDTO token2 = await api.OAuthTokenManager.GetToken();

            Assert.AreNotEqual(token1.access_token, token2.access_token);

            api.Config.ClientId       = "sdk-unit-tests";
            api.Config.ClientPassword = "******";
            api.Config.BaseUrl        = "https://api.sandbox.mangopay.com";

            OAuthTokenDTO token3 = await api.OAuthTokenManager.GetToken();

            Assert.AreEqual(token1.access_token, token3.access_token);
        }
        public void Test_StandardUseToken()
        {
            this.Api.Users.GetAll();
            OAuthTokenDTO token = this.Api.OAuthTokenManager.GetToken();

            this.Api.Users.GetAll();

            Assert.AreEqual(token.access_token, this.Api.OAuthTokenManager.GetToken().access_token);
        }
        public void Test_ShareTokenBetweenInstances()
        {
            MangoPayApi api = this.BuildNewMangoPayApi();

            OAuthTokenDTO token1 = this.Api.OAuthTokenManager.GetToken();
            OAuthTokenDTO token2 = api.OAuthTokenManager.GetToken();

            Assert.AreEqual(token1.access_token, token2.access_token);
        }
Beispiel #5
0
        private async Task <OAuthTokenDTO> StoreAndReturnToken(HttpResponseMessage response)
        {
            var responseStream = await response.Content.ReadAsStreamAsync();

            _token = JsonSerializer.DeserializeAsync <OAuthTokenDTO>(responseStream).Result;

            _logger.LogInformation("Authenticating");
            return(_token);
        }
        public async Task Test_ShareTokenBetweenInstances()
        {
            MangoPayApi api = this.BuildNewMangoPayApi();

            OAuthTokenDTO token1 = await this.Api.OAuthTokenManager.GetTokenAsync();

            OAuthTokenDTO token2 = await api.OAuthTokenManager.GetTokenAsync();

            Assert.AreEqual(token1.access_token, token2.access_token);
        }
Beispiel #7
0
        /// <summary>Gets the current authorization token.
        /// In the very first call, this method creates a new token before returning.
        /// If currently stored token is expired, this method creates a new one.
        /// </summary>
        /// <returns>Valid OAuthToken instance.</returns>
        public OAuthTokenDTO GetToken()
        {
            OAuthTokenDTO token = _storageStrategy.Get();

            if (token == null || token.IsExpired())
            {
                StoreToken(this._root.AuthenticationManager.CreateToken());
            }

            return(_storageStrategy.Get());
        }
Beispiel #8
0
        /// <summary>Gets the current authorization token.
        /// In the very first call, this method creates a new token before returning.
        /// If currently stored token is expired, this method creates a new one.
        /// </summary>
        /// <returns>Valid OAuthToken instance.</returns>
        public async Task <OAuthTokenDTO> GetToken()
        {
            OAuthTokenDTO token = _storageStrategy.Get(GetEnvKey());

            if (token == null || token.IsExpired())
            {
                StoreToken(await this._root.AuthenticationManager.CreateToken());
            }

            return(_storageStrategy.Get(GetEnvKey()));
        }
 /// <summary>Stores authorization token passed as an argument.</summary>
 /// <param name="token">Token instance to be stored.</param>
 /// <param name="envKey">Environment key for token.</param>
 public void Store(OAuthTokenDTO token, string envKey)
 {
     if (_oAuthToken.ContainsKey(envKey))
     {
         _oAuthToken[envKey] = token;
     }
     else
     {
         _oAuthToken.Add(envKey, token);
     }
 }
Beispiel #10
0
 /// <summary>Gets the currently stored token.</summary>
 /// <param name="envKey">Environment key for token.</param>
 /// <returns>Currently stored token instance or null.</returns>
 public OAuthTokenDTO Get(string envKey)
 {
     try
     {
         OAuthTokenDTO token = OAuthTokenDTO.Deserialize(File.ReadAllText(GetFilePath(envKey)));
         return(token);
     }
     catch
     {
         return(null); // it's not an error: e.g. file not found because not stored yet
     }
 }
Beispiel #11
0
        public async Task Test_StandardUseToken()
        {
            await this.Api.Users.GetAll();

            OAuthTokenDTO token = await this.Api.OAuthTokenManager.GetToken();

            await this.Api.Users.GetAll();

            var temp = await this.Api.OAuthTokenManager.GetToken();

            Assert.AreEqual(token.access_token, temp.access_token);
        }
        public void Test_ForceToken()
        {
            OAuthTokenDTO oldToken = this.Api.OAuthTokenManager.GetToken();
            OAuthTokenDTO newToken = this.Api.AuthenticationManager.CreateToken();

            Assert.IsFalse(oldToken.access_token == newToken.access_token);

            this.Api.OAuthTokenManager.StoreToken(newToken);
            OAuthTokenDTO storedToken = this.Api.OAuthTokenManager.GetToken();

            Assert.AreEqual(newToken.access_token, storedToken.access_token);
        }
Beispiel #13
0
        /// <summary>Gets the current authorization token.
        /// In the very first call, this method creates a new token before returning.
        /// If currently stored token is expired, this method creates a new one.
        /// </summary>
        /// <returns>Valid OAuthToken instance.</returns>
        public OAuthTokenDTO GetToken()
        {
            OAuthTokenDTO token = _storageStrategy.Get(GetEnvKey());

            if (token == null || token.IsExpired())
            {
                var result = this._root.AuthenticationManager.CreateToken();
                StoreToken(result);
            }

            return(_storageStrategy.Get(GetEnvKey()));
        }
        private async Task <Dictionary <String, String> > GetHttpHeaderStrongAsync()
        {
            OAuthTokenDTO token = await _root.OAuthTokenManager.GetTokenAsync();

            if (token == null || String.IsNullOrEmpty(token.access_token) || String.IsNullOrEmpty(token.token_type))
            {
                throw new Exception("OAuth token is not created (or is invalid) for strong authentication");
            }

            return(new Dictionary <String, String>
            {
                { Constants.AUTHORIZATION, token.token_type + " " + token.access_token }
            });
        }
Beispiel #15
0
        /// <summary>Gets the new token used for requests authentication.</summary>
        /// <returns>OAuth object with token information.</returns>
        public OAuthTokenDTO CreateToken()
        {
            var endPoint = GetApiEndPoint(MethodKey.AuthenticationOAuth);
            Dictionary <String, String> requestData = new Dictionary <String, String>
            {
                { Constants.GRANT_TYPE, Constants.CLIENT_CREDENTIALS }
            };

            RestTool             restTool   = new RestTool(this._root, false);
            AuthenticationHelper authHelper = new AuthenticationHelper(_root);

            restTool.AddRequestHttpHeader(Constants.HOST, (new Uri(_root.Config.BaseUrl)).Host);
            restTool.AddRequestHttpHeader(Constants.AUTHORIZATION, String.Format("{0} {1}", Constants.BASIC, authHelper.GetHttpHeaderBasicKey()));
            restTool.AddRequestHttpHeader(Constants.CONTENT_TYPE, Constants.APPLICATION_X_WWW_FORM_URLENCODED);

            OAuthTokenDTO response = restTool.Request <OAuthTokenDTO, OAuthTokenDTO>(endPoint, requestData);

            return(response);
        }
 public void Store(OAuthTokenDTO token)
 {
     _oAuthToken = token;
 }
Beispiel #17
0
 /// <summary>Stores authorization token passed as an argument in the underlying storage strategy implementation.</summary>
 /// <param name="token">Token instance to be stored.</param>
 public void StoreToken(OAuthTokenDTO token)
 {
     _storageStrategy.Store(token);
 }
Beispiel #18
0
        /// <summary>Stores authorization token passed as an argument.</summary>
        /// <param name="token">Token instance to be stored.</param>
        /// <param name="envKey">Environment key for token.</param>
        public void Store(OAuthTokenDTO token, string envKey)
        {
            string serializedToken = token.Serialize();

            File.WriteAllText(GetFilePath(envKey), serializedToken);
        }
Beispiel #19
0
 /// <summary>Stores authorization token passed as an argument in the underlying storage strategy implementation.</summary>
 /// <param name="token">Token instance to be stored.</param>
 public void StoreToken(OAuthTokenDTO token)
 {
     _storageStrategy.Store(token, GetEnvKey());
 }