private async Task <AuthenticationResult> ParseSuccessResponseAsync(Uri targetUri, HttpResponseMessage response)
        {
            IExtendedCredential token = null;
            string responseText       = await response.Content.ReadAsStringAsync();

            Match tokenMatch;

            // TODO use compiled regex, switch regex based on Uri Bbc vs BbS
            if ((tokenMatch = Regex.Match(responseText, BitbucketServerConstants.PersonalAccessTokenRegexCommand
                                          ,
                                          RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)).Success &&
                tokenMatch.Groups.Count > 2)
            {
                var    userName  = tokenMatch.Groups[1].Value;
                string tokenText = tokenMatch.Groups[2].Value;
                token = new BearerCredential(userName, tokenText);
            }

            if (token == null)
            {
                _trace.WriteLine($"Authentication for '{targetUri}' failed.");
                return(new AuthenticationResult(AuthenticationResultType.Failure));
            }
            else
            {
                _trace.WriteLine($"Authentication success: new personal access token for '{targetUri}' created.");
                return(new AuthenticationResult(AuthenticationResultType.Success, token));
            }
        }
Beispiel #2
0
        private async Task <AuthenticationResult> ParseAquireTokenSuccessResponseAsync(Uri targetUri, HttpResponseMessage response, IExtendedCredential sourceCredentials)
        {
            IExtendedCredential token = null;
            string responseText       = await response.Content.ReadAsStringAsync();

            Match tokenMatch;

            // TODO use compiled regex, switch regex based on Uri Bbc vs BbS
            if ((tokenMatch = Regex.Match(responseText, BitbucketServerConstants.PersonalAccessTokenRegexCommand
                                          ,
                                          RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)).Success &&
                tokenMatch.Groups.Count > 2)
            {
                var    userName  = tokenMatch.Groups[1].Value;
                string tokenText = tokenMatch.Groups[2].Value;
                token = new BearerCredential(userName, tokenText);
            }

            // Cloud Basic Auth nominal data check from the JSON response to 2.0/user
            if (Constants.Http.WwwAuthenticateBasicScheme.Equals(sourceCredentials.Scheme) &&
                (tokenMatch = Regex.Match(responseText, BitbucketConstants.UserNameFromJsonRegexCommand,
                                          RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)).Success &&
                tokenMatch.Groups.Count > 1)
            {
                var userName = tokenMatch.Groups[1].Value;
                token = new BaseAuthCredential(userName, sourceCredentials.Password);
            }

            if (token == null)
            {
                _trace.WriteLine($"Authentication for '{targetUri}' failed.");
                return(new AuthenticationResult(AuthenticationResultType.Failure));
            }
            else
            {
                _trace.WriteLine($"Authentication success: new personal access token for '{targetUri}' created.");
                return(new AuthenticationResult(AuthenticationResultType.Success, token));
            }
        }
 private static BearerCredential GetBearerCredentialsFromRequest(HttpRequestBase request)
 {
     if (request == null)
     {
         throw new ApplicationException("The Http Request is null");
     }
     else
     {
         string authorizationHeader = request.Headers["Authorization"];
         if (string.IsNullOrEmpty(authorizationHeader))
         {
             throw new HttpException((int)HttpStatusCode.BadRequest, "The Http authorization header was not provided");
         }
         else
         {
             BearerCredential bearer = new BearerCredential() { AccessToken = ASCIIEncoding.ASCII.GetString(Convert.FromBase64String(authorizationHeader.Substring(7))) };
             return bearer;
         }
     }
 }