Ejemplo n.º 1
0
        private void SendOAuth(ServiceAccountCredential.OAuthRequest request)
        {
            Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("Sending OAuth request to {0}", base.TokenServerUrl));
            FirebaseHttpRequest firebaseHttpRequest = Services.HttpFactory.OpenConnection(new Uri(base.TokenServerUrl));

            firebaseHttpRequest.SetRequestMethod("POST");
            firebaseHttpRequest.SetRequestProperty(FirebaseHttpRequest.HeaderContentType, "application/x-www-form-urlencoded");
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("grant_type=");
            stringBuilder.Append("urn:ietf:params:oauth:grant-type:jwt-bearer");
            stringBuilder.Append("&");
            stringBuilder.Append("assertion=");
            stringBuilder.Append(request.Assertion);
            byte[] bytes = Encoding.ASCII.GetBytes(stringBuilder.ToString());
            firebaseHttpRequest.OutputStream.Write(bytes, 0, bytes.Length);
            if (firebaseHttpRequest.ResponseCode >= 200 && firebaseHttpRequest.ResponseCode < 300)
            {
                Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("OAuth request to {0} completed with code {1}", base.TokenServerUrl, firebaseHttpRequest.ResponseCode.ToString()));
                StreamReader streamReader = new StreamReader(firebaseHttpRequest.InputStream);
                request.ResponseBody = streamReader.ReadToEnd();
                Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("OAuth request to {0} completed with msg {1}", base.TokenServerUrl, request.ResponseBody));
            }
            else
            {
                Services.Logging.LogMessage(PlatformLogLevel.Error, string.Format("OAuth request to {0} failed with code {1}", base.TokenServerUrl, firebaseHttpRequest.ResponseCode.ToString()));
                StreamReader streamReader2 = new StreamReader(firebaseHttpRequest.ErrorStream);
                string       arg           = streamReader2.ReadToEnd();
                Services.Logging.LogMessage(PlatformLogLevel.Debug, string.Format("OAuth request to {0} failed with msg {1}", base.TokenServerUrl, arg));
                request.ResponseBody = null;
            }
        }
Ejemplo n.º 2
0
        public override string GetAccessTokenForRequestSync(CancellationToken taskCancellationToken)
        {
            string assertion = this.CreateAssertionFromPayload(this.CreatePayload());

            ServiceAccountCredential.OAuthRequest oauthRequest = new ServiceAccountCredential.OAuthRequest
            {
                Assertion = assertion
            };
            this.SendOAuth(oauthRequest);
            if (string.IsNullOrEmpty(oauthRequest.ResponseBody))
            {
                return(null);
            }
            IDictionary <string, object> dictionary = (IDictionary <string, object>)Json.Deserialize(oauthRequest.ResponseBody);
            object obj = null;

            if (dictionary != null)
            {
                dictionary.TryGetValue("access_token", out obj);
            }
            return((obj == null) ? null : obj.ToString());
        }