コード例 #1
0
ファイル: OAuthHelpers.cs プロジェクト: BSalita/Woundify
        private async System.Threading.Tasks.Task RenewAccessTokenAsync()
        {
            ServiceResponse sr = new ServiceResponse();
            await HttpMethods.CallApiAuthAsync(sr, accessUri, grant, headers);

            accessTokenInfo = Newtonsoft.Json.JsonConvert.DeserializeObject <AccessTokenInfo>(sr.ResponseString); // warning: thread unsafe
            Log.WriteLine("Renewed token:" + accessTokenInfo.access_token);
        }
コード例 #2
0
        private static List <Tuple <string, string> > MakeHeaders(Settings.Request request)
        {
            List <Tuple <string, string> > headers = new List <Tuple <string, string> >();

            if (request == null || request.headers == null)
            {
                return(headers);
            }
            foreach (Settings.Header h in request.headers)
            {
                // TODO: need to expand request headers to include any header. Use Value instead of "Generic" or concrete name (Accept, ContentType)
                switch (h.Name)
                {
                case "BearerAuthentication":
                    AccessTokenInfo accessTokenInfo = ImageAuth.PerformAuthenticationAsync(request, h).Result;     // TODO: make this method async so don't have to do Wait?

                    headers.Add(new Tuple <string, string>("Authorization", "Bearer " + accessTokenInfo.access_token));
                    break;

                case "BasicAuthentication":
                    string userpass = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", h.BasicAuthentication.username, h.BasicAuthentication.password).Replace('+', '-').Replace('/', '_')));
                    headers.Add(new Tuple <string, string>("Authorization", "Basic " + userpass));
                    break;

                case "HoundifyAuthentication":     // will be passed in but must skip, don't let default
                    break;

                case "OcpApimSubscriptionKey":
                    headers.Add(new Tuple <string, string>("Ocp-Apim-Subscription-Key", h.OcpApimSubscriptionKey));    // needs rewrite. see above.
                    break;

                default:
                    if (h.Generic == null)
                    {
                        throw new Exception("Header: Generic is null");
                    }
                    headers.Add(new Tuple <string, string>(h.Name, h.Generic));
                    break;
                }
            }
            return(headers);
        }
コード例 #3
0
ファイル: OAuthHelpers.cs プロジェクト: BSalita/Woundify
        public async System.Threading.Tasks.Task <AccessTokenInfo> PerformAuthenticationAsync(Settings.Request request, Settings.Header h)
        {
            Settings.BearerAuthentication bearer = h.BearerAuthentication;
            if (request == null || h == null)
            {
                throw new Exception("request/h is null");
            }
            if (h.OcpApimSubscriptionKey != null) // ClarifAi, OcpApimSubscriptionKey
            {
                Settings.BearerAuthentication BearerAuth = h.BearerAuthentication;
                Uri accessUri = new Uri(BearerAuth.uri);
                // todo: this only works for Microsoft APIs. Make code conditional on Microsoft? Break out as separate API to be execute first? Change headers in json file?
                headers = new System.Collections.Generic.List <Tuple <string, string> >()
                {
                    new Tuple <string, string>("Content-Type", "application/x-www-form-urlencoded"),
                    new Tuple <string, string>("Ocp-Apim-Subscription-Key", h.OcpApimSubscriptionKey)        // TODO: need dictionary lookup instead of hardcoding
                };
                ServiceResponse sr = new ServiceResponse();
                HttpMethods.CallApiAuthAsync(sr, accessUri, "", headers).Wait();
                accessTokenInfo = new AccessTokenInfo();
                accessTokenInfo.access_token = sr.ResponseString;
            }
            else if (h.BearerAuthentication.clientID != null && h.BearerAuthentication.clientSecret != null) // Microsoft
            {
                string clientID     = bearer.clientID;
                string clientSecret = bearer.clientSecret;
                //string scope = bearer.scope;

                this.request = request;
                System.Collections.Generic.List <Tuple <string, string> > grantSubstitutes = new System.Collections.Generic.List <Tuple <string, string> >()
                {
                    new Tuple <string, string>("{clientID}", System.Web.HttpUtility.UrlEncode(clientID)),
                    new Tuple <string, string>("{clientSecret}", System.Web.HttpUtility.UrlEncode(clientSecret)),
                    //new Tuple<string, string>("{scope}", System.Web.HttpUtility.UrlEncode(scope)),
                };
                grant = bearer.grant;
                foreach (Tuple <string, string> r in grantSubstitutes)
                {
                    grant = grant.Replace(r.Item1, r.Item2);
                }
                accessUri = new Uri(bearer.uri);
                headers   = new System.Collections.Generic.List <Tuple <string, string> >()
                {
                    new Tuple <string, string>("Content-Type", "application/x-www-form-urlencoded")
                };
                ServiceResponse sr = new ServiceResponse();
                await HttpMethods.CallApiAuthAsync(sr, accessUri, grant, headers);

                accessTokenInfo = Newtonsoft.Json.JsonConvert.DeserializeObject <AccessTokenInfo>(sr.ResponseString);

                // renew the token every specfied minutes
                accessTokenRenewer = new System.Threading.Timer(new System.Threading.TimerCallback(OnTokenExpiredCallbackAsync),
                                                                this,
                                                                TimeSpan.FromMinutes(RefreshTokenDuration),
                                                                TimeSpan.FromMilliseconds(-1));
            }
            else if (h.BearerAuthentication.bearer != null) // Wit.Ai
            {
                accessTokenInfo.access_token = h.BearerAuthentication.bearer;
            }
            else
            {
                throw new Exception("Unknown Bearer Authentication");
            }
            return(accessTokenInfo);
        }