예제 #1
0
        /// <inheritdoc/>
        public virtual async Task <CloudStorageToken> FetchTokenAsync(string redirectedUrl, string state, string codeVerifier)
        {
            if (string.IsNullOrWhiteSpace(redirectedUrl))
            {
                throw new ArgumentNullException(nameof(redirectedUrl));
            }
            if (string.IsNullOrWhiteSpace(state))
            {
                throw new ArgumentNullException(nameof(state));
            }

            AuthorizationResponse response = OAuth2Utils.ParseAuthorizationResponseUrl(redirectedUrl);

            // Check whether the user denied access
            if (!response.IsAccessGranted)
            {
                return(null);
            }

            // Verify state
            if (response.State != state)
            {
                throw new CloudStorageException("The authorization response has a wrong state, this indicates a hacking attempt.", null);
            }

            // Determine flow type
            AuthorizationFlow flow;

            if (!string.IsNullOrWhiteSpace(response.Token))
            {
                flow = AuthorizationFlow.Token;
            }
            else if (!string.IsNullOrWhiteSpace(response.Code))
            {
                flow = AuthorizationFlow.Code;
            }
            else
            {
                throw new CloudStorageException("The authorization response is neither form a token-flow, nor from a code-flow request.", null);
            }

            switch (flow)
            {
            case AuthorizationFlow.Token:
                return(new CloudStorageToken
                {
                    AccessToken = response.Token,
                    ExpiryDate = null,
                    RefreshToken = null
                });

            case AuthorizationFlow.Code:
                return(await ExchangeCodeForTokenAsync(response.Code, codeVerifier));

            default:
                return(null);    // Never happens
            }
        }
예제 #2
0
        public void ParseRealWorldDropboxSuccessResponse()
        {
            string redirectUrl             = "com.example.myapp://oauth2redirect/#access_token=vQGkdNzLZ9AAAAAAApZW8zLyjRRRRRcic2MqEx1A1AdyaPcLdbKKOLNg2I8z0we-&token_type=bearer&state=68D8kUO0ubb78C8k&uid=111111111&account_id=abcd%3AAADucBH8TWrbYMWUHlrWQ4TGdcyC55pzBKk";
            AuthorizationResponse response = OAuth2Utils.ParseAuthorizationResponseUrl(redirectUrl);

            Assert.IsTrue(response.IsAccessGranted);
            Assert.AreEqual("vQGkdNzLZ9AAAAAAApZW8zLyjRRRRRcic2MqEx1A1AdyaPcLdbKKOLNg2I8z0we-", response.Token);
            Assert.IsNull(response.Code);
            Assert.AreEqual("68D8kUO0ubb78C8k", response.State);
            Assert.IsNull(response.Error);
        }
예제 #3
0
        public void ParseRealWorldGoogleSuccessResponse()
        {
            string redirectUrl             = "com.example.myapp:/oauth2redirect/?state=AJ7CQLlJEwNn2AVL&code=4/aQHgCtVfeTg--SEAyJ6pYHvcCtZZZZZckvGcT5OpPjNuEEEEcvUJzQSaAALzD_DSfenHwHXItOE2Ax55j25-bbY&scope=https://www.googleapis.com/auth/drive.appdata";
            AuthorizationResponse response = OAuth2Utils.ParseAuthorizationResponseUrl(redirectUrl);

            Assert.IsTrue(response.IsAccessGranted);
            Assert.AreEqual("4/aQHgCtVfeTg--SEAyJ6pYHvcCtZZZZZckvGcT5OpPjNuEEEEcvUJzQSaAALzD_DSfenHwHXItOE2Ax55j25-bbY", response.Code);
            Assert.IsNull(response.Token);
            Assert.AreEqual("AJ7CQLlJEwNn2AVL", response.State);
            Assert.IsNull(response.Error);
        }
예제 #4
0
        public void ParseRealWorldGoogleRejectResponse()
        {
            string redirectUrl             = "com.example.myapp:/oauth2redirect/?error=access_denied&state=AJ7CQLlJEwNn2AVL";
            AuthorizationResponse response = OAuth2Utils.ParseAuthorizationResponseUrl(redirectUrl);

            Assert.IsFalse(response.IsAccessGranted);
            Assert.IsNull(response.Token);
            Assert.IsNull(response.Code);
            Assert.AreEqual("AJ7CQLlJEwNn2AVL", response.State);
            Assert.AreEqual(AuthorizationResponseError.AccessDenied, response.Error);
        }
예제 #5
0
        public void ParseRealWorldDropboxRejectResponse()
        {
            string redirectUrl             = "com.example.myapp://oauth2redirect/#state=AJ7CQLlJEwNn2AVL&error_description=The+user+chose+not+to+give+your+app+access+to+their+Dropbox+account.&error=access_denied";
            AuthorizationResponse response = OAuth2Utils.ParseAuthorizationResponseUrl(redirectUrl);

            Assert.IsFalse(response.IsAccessGranted);
            Assert.IsNull(response.Token);
            Assert.IsNull(response.Code);
            Assert.AreEqual("AJ7CQLlJEwNn2AVL", response.State);
            Assert.AreEqual(AuthorizationResponseError.AccessDenied, response.Error);
        }