コード例 #1
0
        private OauthAccessToken ExecuteTokenRequest(Dictionary<string, string> p, Authentication authentication, string url, bool revoke = false)
        {
            OauthAccessToken token = null;

            if (authentication == null)
            {
                authentication = GetDefaultAuthentication();
            }

            string requestBody = BuildParamString(p, "POST");

            string privateKey = authentication.PrivateApiKey;
            string publicKey = authentication.PublicApiKey;
            string signature = JwsEncode(url, requestBody, authentication);

            HttpWebRequest request = CreateWebRequest(url, requestBody, "POST", privateKey, publicKey);
            request.ContentType = "application/x-www-form-urlencoded";
            request.AllowAutoRedirect = false;

            try
            {
                string result = ExecuteRequest(request, requestBody, signature);

                if (!revoke)
                {
                    token = (OauthAccessToken)JsonConvert.DeserializeObject(result, typeof(OauthAccessToken));
                }
                else
                {
                    OauthRevokeAccessToken t = (OauthRevokeAccessToken)JsonConvert.DeserializeObject(result, typeof(OauthRevokeAccessToken));
                    token = new OauthAccessToken();
                    token.AccessToken = t.TokenRevoked;
                }

                token.PrivateApiKey = privateKey;
                token.PublicApiKey = publicKey;
            }
            catch (InvalidRequestException ex)
            {
                JObject o = (JObject)ex.ErrorData;

                string error = o.GetValue("error").Value<string>();
                string errorDescription = o.GetValue("error_description").Value<string>();
                throw new OauthException(error, errorDescription);
            }

            return token;
        }
コード例 #2
0
        private string JwsEncode(string uri, string payload, Authentication authentication)
        {
            Dictionary<String, String> jwsHeader = new Dictionary<string, string>();
            string privateKey = authentication.PrivateApiKey;
            string publicKey = authentication.PublicApiKey;
            string oauthToken = null;

            if (authentication is OauthAccessToken)
            {
                oauthToken = ((OauthAccessToken)authentication).AccessToken;
            }

            jwsHeader["typ"] = "JWS";
            jwsHeader["kid"] = publicKey;
            jwsHeader["alg"] = "HS256";
            jwsHeader[HEADER_TIME] = CurrentTimeMillis().ToString();
            jwsHeader[HEADER_URI] = uri;
            byte[] b = new byte[8];
            rng.GetBytes(b);
            jwsHeader[HEADER_NONCE] = Convert.ToBase64String(b);

            if (oauthToken != null)
            {
                jwsHeader[HEADER_OAUTH] = oauthToken;
            }

            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.NullValueHandling = NullValueHandling.Ignore;
            string header = JsonConvert.SerializeObject(jwsHeader, settings);

            if (payload == null)
            {
                payload = "";
            }

            string signatureBaseString = ToBase64UrlSafe(header) + "." + ToBase64UrlSafe(payload);
            string signatureString = JwsSign(privateKey, signatureBaseString);
            string signed = signatureBaseString + "." + signatureString;

            return signed;
        }
コード例 #3
0
        public Object Update(Object obj, Authentication authentication = null)
        {
            Type type = obj.GetType();

            Dictionary<string, Object> objectMap = new Dictionary<string, Object>();
            objectMap["id"] = (string)obj.GetType().GetProperty("Id").GetValue(obj, null);
            objectMap["object"] = obj;

            return Execute(type, ApiAction.Update, objectMap, authentication);
        }
コード例 #4
0
        private Object Execute(Type type, ApiAction action, Dictionary<string, Object> objectMap = null, Authentication authentication = null)
        {
            if (authentication == null)
            {
                authentication = GetDefaultAuthentication();
            }

            string url = null;
            string requestBody = null;
            string signature = null;
            string privateKey = authentication.PrivateApiKey;
            string publicKey = authentication.PublicApiKey;

            JsonSerializerSettings settings = new JsonSerializerSettings();
            settings.NullValueHandling = NullValueHandling.Ignore;

            switch (action)
            {
                case ApiAction.List:
                    Dictionary<string, string> queryParams = new Dictionary<string, string>();
                    queryParams["max"] = (string)objectMap["max"];
                    queryParams["offset"] = (string)objectMap["offset"];

                    if (objectMap["sorting"] != null)
                    {
                        Dictionary<string, string> sorting = (Dictionary<string, string>)objectMap["sorting"];
                        foreach (var key in sorting.Keys)
                        {
                            queryParams["sorting[" + key + "]"] = sorting[key];
                        }
                    }

                    if (objectMap["filter"] != null)
                    {
                        Dictionary<string, string> filter = (Dictionary<string, string>)objectMap["filter"];
                        foreach (var key in filter.Keys)
                        {
                            queryParams["filter[" + key + "]"] = filter[key];
                        }
                    }

                    url = GetUrl(type, action, publicKey, null, queryParams);

                    signature = JwsEncode(url, null, authentication);
                    break;

                case ApiAction.Find:
                    url = GetUrl(type, action, publicKey, (string)objectMap["id"]);
                    signature = JwsEncode(url, null, authentication);
                    break;
                case ApiAction.Delete:
                    url = GetUrl(type, action, publicKey, (string)objectMap["id"]);
                    signature = JwsEncode(url, null, authentication);
                    break;
                case ApiAction.Create:
                    url = GetUrl(type, action, publicKey, null);

                    requestBody = JsonConvert.SerializeObject(objectMap["object"], settings);
                    requestBody = JwsEncode(url, requestBody, authentication);
                    break;
                case ApiAction.Update:
                    url = GetUrl(type, action, publicKey, (string)objectMap["id"]);
                    requestBody = JsonConvert.SerializeObject(objectMap["object"], settings);
                    requestBody = JwsEncode(url, requestBody, authentication);
                    break;
            }

            HttpWebRequest request = CreateWebRequest(url, requestBody, ActionToMethodMap[action], privateKey, publicKey);

            string result = ExecuteRequest(request, requestBody, signature);
            return ConvertResponse(result, type, action);
        }
コード例 #5
0
 public OauthAccessToken RequestAccessToken(OauthAccessTokenRequest oauthRequest, Authentication authentication = null)
 {
     Dictionary<string, string> p = new Dictionary<string, string>();
     p["grant_type"] = "authorization_code";
     p["code"] = oauthRequest.Code;
     p["redirect_uri"] = oauthRequest.RedirectUri;
     return ExecuteTokenRequest(p, authentication, OauthBaseUrl + "/token");
 }
コード例 #6
0
        public Object List(Type type, Dictionary<string, string> sorting = null, Dictionary<string, string> filter = null, int max = DEFAULT_MAX, int offset = 0, Authentication authentication = null)
        {
            Dictionary<string, Object> objectMap = new Dictionary<string, Object>();
            objectMap["max"] = max.ToString();
            objectMap["offset"] = offset.ToString();
            objectMap["sorting"] = sorting;
            objectMap["filter"] = filter;

            return Execute(type, ApiAction.List, objectMap, authentication);
        }
コード例 #7
0
        public Object Find(Type type, String id, Authentication authentication = null)
        {
            Dictionary<string, Object> objectMap = new Dictionary<string, Object>();
            objectMap["id"] = id;

            return Execute(type, ApiAction.Find, objectMap, authentication);
        }
コード例 #8
0
        public Object Find(Object obj, Authentication authentication = null)
        {
            Type type = obj.GetType();
            string id = (string)obj.GetType().GetProperty("Id").GetValue(obj, null);

            return Find(type, id, authentication);
        }
コード例 #9
0
        public Object Create(Object obj, Authentication authentication = null)
        {
            Dictionary<string, Object> objectMap = new Dictionary<string, Object>();
            objectMap["object"] = obj;

            return Execute(obj.GetType(), ApiAction.Create, objectMap, authentication);
        }