static public Tuple <bool, OculiOAuth2Token, object> Deserialize(IRestResponse _response)
 {
     try
     {
         object _response_object = JsonConvert.DeserializeObject <OculiOAuth2AuthResponse>(_response.Content, new JsonSerializerSettings
         {
             MissingMemberHandling = MissingMemberHandling.Ignore,
             NullValueHandling     = NullValueHandling.Ignore,
             Error = new OculiException().DeserializationExpection
         });
         if (_response.StatusCode == HttpStatusCode.OK)
         {
             OculiOAuth2Token _token = OculiTokenFactory.SetToken(_response);
             return(Tuple.Create(true, new OculiOAuth2Token(), _response_object));
         }
         else if (_response.StatusCode == HttpStatusCode.Unauthorized)
         {
             OculiOAuth2Token _token = OculiTokenFactory.SetToken(_response);
             return(Tuple.Create(false, _token, _response_object));
         }
         else
         {
             throw new Exception("Platform credentials incorrect");
         }
     }
     catch (Exception ex)
     {
         throw new Exception(String.Format("Cannot deserialize object : {0}", ex.GetBaseException().Message));
     }
 }
        static public OculiOAuth2Token SetToken(IRestResponse _response)
        {
            OculiOAuth2Token _token = new OculiOAuth2Token();

            _token.accesstoken = _response.Headers.FirstOrDefault(x => x.Name == "access_token")?.Value?.ToString();
            _token.expiry      = Convert.ToInt64(_response.Headers.FirstOrDefault(x => x.Name == "expiry")?.Value?.ToString());
            return(_token);
        }
Beispiel #3
0
 public static OculiOAuth2Token ApplyCryptor(this OculiOAuth2Token toEncrypt, Func <string, string> cryptor)
 {
     Invariant.ArgumentNotNull((object)toEncrypt, "toEncrypt");
     Invariant.ArgumentNotNull((object)cryptor, "cryptor");
     return(new OculiOAuth2Token()
     {
         accesstoken = cryptor(toEncrypt.accesstoken), uid = toEncrypt.uid, coreengine_id = toEncrypt.coreengine_id
     });
 }
        static public void StoreToken(OculiOAuth2Token _token)
        {
            TripleDESOAuth2TokenCryptor _token_cryptor       = new TripleDESOAuth2TokenCryptor();
            ICoreEngineSettings         _coreengine_settings = new CoreEngineSettings();
            OculiOAuth2Token            _encrypted_token     = _token_cryptor.Encrypt(_token);

            _coreengine_settings.TokenAccessCode = _encrypted_token.accesstoken;
            _coreengine_settings.TokenExpiry     = _token.expiry;
        }
        static public OculiOAuth2Token RetrieveToken()
        {
            ICoreEngineSettings _coreengine_settings = new CoreEngineSettings();

            OculiOAuth2Token _encrypted_token = new OculiOAuth2Token();

            _encrypted_token.accesstoken = _coreengine_settings.TokenAccessCode;
            _encrypted_token.expiry      = _coreengine_settings.TokenExpiry;
            TripleDESOAuth2TokenCryptor _token_cryptor   = new TripleDESOAuth2TokenCryptor();
            OculiOAuth2Token            _decrypted_token = _token_cryptor.Decrypt(_encrypted_token);

            return(_decrypted_token);
        }
        static public RestRequest SetTokenRequest(RestRequest _request)
        {
            ICoreEngineSettings _coreengine_settings = new CoreEngineSettings();
            OculiOAuth2Token    _decrypted_token     = new OculiOAuth2Token();

            _decrypted_token = RetrieveToken();
            Dictionary <string, string> _tokens = _request.Parameters.ToDictionary(x => x.Name, x => x.Value.ToString());

            Util.AddOrReplace(_tokens, "username", _coreengine_settings.AccessKey);
            Util.AddOrReplace(_tokens, "access_token", _decrypted_token.accesstoken);
            Util.AddOrReplace(_tokens, "coreengine_id", _coreengine_settings.CoreEngineID);
            _tokens.ForEach(x => {
                if (_request.Parameters.Any(z => z.Name == x.Key))
                {
                    _request.Parameters.FirstOrDefault(z => z.Name == x.Key).Value = x.Value;
                }
                else
                {
                    _request.AddHeader(x.Key, x.Value);
                }
            });
            return(_request);
        }
Beispiel #7
0
 public static OculiOAuth2Token EncryptUserScope(this OculiOAuth2Token toEncrypt)
 {
     return(toEncrypt.ApplyCryptor(new Func <string, string>(Cryptor.Encrypt)));
 }
Beispiel #8
0
 public static OculiOAuth2Token DecryptTripleDES(this OculiOAuth2Token toDecrypt)
 {
     return(toDecrypt.ApplyCryptor(new Func <string, string>(Cryptor.DecryptTripleDES)));
 }
Beispiel #9
0
 public static OculiOAuth2Token DecryptMachineScope(this OculiOAuth2Token toDecrypt)
 {
     return(toDecrypt.ApplyCryptor(new Func <string, string>(Cryptor.DecryptMachineScope)));
 }
 public OculiOAuth2Token Encrypt(OculiOAuth2Token plain)
 {
     return(plain.EncryptTripleDES());
 }
 public OculiOAuth2Token Decrypt(OculiOAuth2Token cipher)
 {
     return(cipher.DecryptTripleDES());
 }