Example #1
0
        public async Task <IResponseResult <Application> > WhoAmIAsync(BasicToken basicToken)
        {
            var url     = $"{this.AuthApiBaseUrl}/whoami";
            var headers = HeaderCollection.Add("Authorization", basicToken.ToString());

            return(await this.ExecuteRequestAsync <Application>(HttpMethod.Get, url, null, headers));
        }
Example #2
0
        public IResponseResult <Application> WhoAmI(BasicToken basicToken)
        {
            var url     = $"{this.BaseUrl}/whoami";
            var headers = HeaderCollection.Add("Authorization", basicToken.ToString());

            return(this.ExecuteRequest <Application>(HttpMethod.Get, url, null, headers));
        }
Example #3
0
        public CommEntityUpdater(WxConfig wxConfig, JzDbContext db)
        {
            _wxConfig  = wxConfig;
            this.db    = db;
            basicToken = db.BasicTokens.FirstOrDefault();
            if (basicToken == null)
            {
                LogService.GetInstance().AddLog("CommEntityUpdater:ctor", null, "Create a new basic token record", "", "Info");
                basicToken = new BasicToken();
                db.BasicTokens.Add(basicToken);
                db.SaveChanges();
            }
            else if (string.IsNullOrEmpty(ComponentKeys.GetInstance().VerifyData.Ticket))
            {
                //load token from db to memory when component ticket is null
                ComponentKeys.GetInstance().VerifyData.Ticket = basicToken.Ticket;
                ComponentKeys.GetInstance().VerifyData.RefreshOn = basicToken.TicketRefreshOn;

                ComponentKeys.GetInstance().AccessData.AccessCode = basicToken.AccessToken;
                ComponentKeys.GetInstance().AccessData.ExpiresIn = basicToken.AccessTokenExpiresIn;
                ComponentKeys.GetInstance().AccessData.RefreshOn = basicToken.AccessTokenRefreshOn;

                ComponentKeys.GetInstance().PreAuthData.PreAuthCode = basicToken.PreAuthCode;
                ComponentKeys.GetInstance().PreAuthData.RefreshOn = basicToken.PreAuthCodeRefreshOn;
                ComponentKeys.GetInstance().PreAuthData.ExpiresIn = basicToken.PreAuthCodeExpiresIn;
            }
        }
Example #4
0
 void NewGame()
 {
     Token = new BasicToken [GameData.MapSizeX, GameData.MapSizeY];
     for (int x = 0; x < GameData.MapSizeX; x++)
     {
         for (int y = 0; y < GameData.MapSizeY; y++)
         {
             Token [x, y] = new BasicToken();
         }
     }
     TurnOfPlayer = 0;
 }
        public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args)
        .ConfigureApi((context, options) =>
        {
            ApiKeyToken apiKeyToken = new ApiKeyToken(context.Configuration["<token>"], timeout: TimeSpan.FromSeconds(1));
            options.AddTokens(apiKeyToken);

            BearerToken bearerToken = new BearerToken(context.Configuration["<token>"], timeout: TimeSpan.FromSeconds(1));
            options.AddTokens(bearerToken);

            BasicToken basicToken = new BasicToken(context.Configuration["<username>"], context.Configuration["<password>"], timeout: TimeSpan.FromSeconds(1));
            options.AddTokens(basicToken);

            HttpSigningConfiguration config       = new HttpSigningConfiguration("<keyId>", "<keyFilePath>", null, new List <string>(), HashAlgorithmName.SHA256, "<signingAlgorithm>", 0);
            HttpSignatureToken httpSignatureToken = new HttpSignatureToken(config, timeout: TimeSpan.FromSeconds(1));
            options.AddTokens(httpSignatureToken);

            OAuthToken oauthToken = new OAuthToken(context.Configuration["<token>"], timeout: TimeSpan.FromSeconds(1));
            options.AddTokens(oauthToken);
        });
Example #6
0
        public async Task <List <KeyValuePair <string, string> > > GetAsanaWorkSpaces(BasicToken input)
        {
            var httpClient = new HttpClient();

            var request = new HttpRequestMessage(HttpMethod.Get, "workspaces");

            httpClient.BaseAddress = new Uri("https://app.asana.com/api/1.0/"); // to do: get uri from appsettings
            httpClient.DefaultRequestHeaders.Add("Accept", "application/json");
            httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {input.Token}");

            var response = await httpClient.SendAsync(request);


            if (response.StatusCode == HttpStatusCode.BadRequest)
            {
                throw new UserFriendlyException("an error occurred");
            }

            var stringData = await response.Content.ReadAsStringAsync();

            var workspaces = JsonConvert.DeserializeObject <BaseAsana <AsanaWorkspace> >(stringData);
            var list       = new List <KeyValuePair <string, string> >();

            workspaces.data.ForEach(a =>
            {
                list.Add(new KeyValuePair <string, string>(a.gid, a.name));
            });

            return(list);
        }
Example #7
0
        private async Task <Utilizer> CheckAuthorizationAsync()
        {
            var token = this.Request.GetTokenFromHeader(out var tokenType);

            if (string.IsNullOrEmpty(token))
            {
                throw ErtisAuthException.AuthorizationHeaderMissing();
            }

            if (string.IsNullOrEmpty(tokenType))
            {
                throw ErtisAuthException.UnsupportedTokenType();
            }

            TokenTypeExtensions.TryParseTokenType(tokenType, out var _tokenType);
            switch (_tokenType)
            {
            case SupportedTokenTypes.None:
                throw ErtisAuthException.UnsupportedTokenType();

            case SupportedTokenTypes.Basic:
                var basicToken             = new BasicToken(token);
                var applicationId          = token.Split(':')[0];
                var getApplicationResponse = await this.applicationService.GetApplicationAsync(applicationId, basicToken);

                if (getApplicationResponse.IsSuccess)
                {
                    var rbacDefinition       = this.Context.GetRbacDefinition(getApplicationResponse.Data.Id);
                    var rbac                 = $"{rbacDefinition.Resource}.{rbacDefinition.Action}";
                    var isPermittedForAction = await this.roleService.CheckPermissionAsync(rbac, basicToken);

                    if (!isPermittedForAction)
                    {
                        throw ErtisAuthException.AccessDenied($"Token owner role is not permitted for this resource/action ({rbac})");
                    }

                    return(new Utilizer
                    {
                        Id = getApplicationResponse.Data.Id,
                        Username = getApplicationResponse.Data.Name,
                        Type = tokenType == "Basic" ? Utilizer.UtilizerType.Application : Utilizer.UtilizerType.User,
                        Role = getApplicationResponse.Data.Role,
                        Token = token,
                        TokenType = _tokenType
                    });
                }
                else
                {
                    var errorMessage = getApplicationResponse.Message;
                    if (ResponseHelper.TryParseError(getApplicationResponse.Message, out var error))
                    {
                        errorMessage = error.Message;
                    }

                    throw ErtisAuthException.Unauthorized(errorMessage);
                }

            case SupportedTokenTypes.Bearer:
                var bearerToken = BearerToken.CreateTemp(token);
                var meResponse  = await this.authenticationService.WhoAmIAsync(bearerToken);

                if (meResponse.IsSuccess)
                {
                    var rbacDefinition       = this.Context.GetRbacDefinition(meResponse.Data.Id);
                    var rbac                 = $"{rbacDefinition.Resource}.{rbacDefinition.Action}";
                    var isPermittedForAction = await this.roleService.CheckPermissionAsync(rbac, bearerToken);

                    if (!isPermittedForAction)
                    {
                        throw ErtisAuthException.AccessDenied($"Token owner role is not permitted for this resource/action ({rbac})");
                    }

                    return(new Utilizer
                    {
                        Id = meResponse.Data.Id,
                        Username = meResponse.Data.Username,
                        Type = tokenType == "Basic" ? Utilizer.UtilizerType.Application : Utilizer.UtilizerType.User,
                        Role = meResponse.Data.Role,
                        Token = token,
                        TokenType = _tokenType
                    });
                }
                else
                {
                    var errorMessage = meResponse.Message;
                    if (ResponseHelper.TryParseError(meResponse.Message, out var error))
                    {
                        errorMessage = error.Message;
                    }

                    throw ErtisAuthException.Unauthorized(errorMessage);
                }

            default:
                throw ErtisAuthException.UnsupportedTokenType();
            }
        }
 public async Task <List <KeyValuePair <string, string> > > GetAsanaWorkSpaces(BasicToken input)
 {
     return(await _asanaManager.GetAsanaWorkSpaces(input));
 }
Example #9
0
        public async ValueTask <Application> WhoAmIAsync(BasicToken basicToken)
        {
            await this.VerifyBasicTokenAsync(basicToken.AccessToken, false);

            return(await this.GetTokenOwnerApplicationAsync(basicToken.AccessToken));
        }