Beispiel #1
0
        public void AssignCondition(Type tokenType)
        {
            GenericToken token = (GenericToken)Activator.CreateInstance(tokenType, Host);

            AssignCondition(token);
        }
Beispiel #2
0
 public abstract bool IsTokenCanBeDiscardedByJam(GenericToken token);
        public virtual bool IsShotAvailable(GenericShip targetShip)
        {
            bool result = true;

            int MinRangeUpdated = WeaponInfo.MinRange;
            int MaxRangeUpdated = WeaponInfo.MaxRange;

            HostShip.CallUpdateWeaponRange(this, ref MinRangeUpdated, ref MaxRangeUpdated);

            if (!State.IsFaceup)
            {
                return(false);
            }

            if (State.UsesCharges && State.Charges == 0)
            {
                return(false);
            }

            ShotInfo shotInfo = new ShotInfo(HostShip, targetShip, this);
            int      range    = shotInfo.Range;

            if (!shotInfo.IsShotAvailable)
            {
                return(false);
            }

            if (range < MinRangeUpdated)
            {
                return(false);
            }
            if (range > MaxRangeUpdated)
            {
                return(false);
            }

            if (WeaponInfo.RequiresToken == typeof(BlueTargetLockToken))
            {
                List <GenericToken> waysToPay = new List <GenericToken>();

                List <char>  letters         = ActionsHolder.GetTargetLocksLetterPairs(HostShip, targetShip);
                GenericToken targetLockToken = HostShip.Tokens.GetToken(typeof(BlueTargetLockToken), letters.FirstOrDefault());
                if (targetLockToken != null)
                {
                    waysToPay.Add(targetLockToken);
                }

                HostShip.CallOnGenerateAvailableAttackPaymentList(waysToPay);

                if (waysToPay.Count == 0)
                {
                    return(false);
                }
            }

            if (WeaponInfo.RequiresToken == typeof(FocusToken))
            {
                if (!HostShip.Tokens.HasToken(typeof(FocusToken)))
                {
                    return(false);
                }
            }

            return(result);
        }
Beispiel #4
0
        // CONDITIONS - don't trigger any abilities

        public void RemoveCondition(Type type)
        {
            GenericToken assignedCondition = GetToken(type);

            RemoveCondition(assignedCondition);
        }
        public virtual bool IsShotAvailable(GenericShip targetShip)
        {
            bool result = true;

            if (isDiscarded)
            {
                return(false);
            }

            int range;

            if (!CanShootOutsideArc)
            {
                Board.ShipShotDistanceInformation shotInfo = new Board.ShipShotDistanceInformation(Host, targetShip, this);
                range = shotInfo.Range;

                if (!shotInfo.InShotAngle)
                {
                    return(false);
                }

                switch (Type)
                {
                case UpgradeType.Missile:
                    if (!shotInfo.CanShootMissiles)
                    {
                        return(false);
                    }
                    break;

                case UpgradeType.Cannon:
                    if (!shotInfo.CanShootCannon)
                    {
                        return(false);
                    }
                    break;

                case UpgradeType.Torpedo:
                    if (!shotInfo.CanShootTorpedoes)
                    {
                        return(false);
                    }
                    break;

                default:
                    break;
                }
            }
            else
            {
                Board.ShipDistanceInformation distanceInfo = new Board.ShipDistanceInformation(Host, targetShip);
                range = distanceInfo.Range;
            }

            if (range < MinRange)
            {
                return(false);
            }
            if (range > MaxRange)
            {
                return(false);
            }

            if (RequiresTargetLockOnTargetToShoot)
            {
                List <GenericToken> waysToPay = new List <GenericToken>();

                char         letter          = Actions.GetTargetLocksLetterPair(Host, targetShip);
                GenericToken targetLockToken = Host.GetToken(typeof(BlueTargetLockToken), letter);
                if (targetLockToken != null)
                {
                    waysToPay.Add(targetLockToken);
                }

                Host.CallOnGenerateAvailableAttackPaymentList(waysToPay);

                if (waysToPay.Count == 0)
                {
                    return(false);
                }
            }

            if (RequiresFocusToShoot)
            {
                if (!Host.HasToken(typeof(FocusToken)))
                {
                    return(false);
                }
            }

            return(result);
        }
 /// <summary>
 /// Adds the provided token to the available tokens in the current connection
 /// </summary>
 /// <param name="tokenAudience">Audience the token is for</param>
 /// <param name="token">The token to add</param>
 internal void AddToken(TokenAudience tokenAudience, GenericToken token)
 {
     AccessTokens[tokenAudience] = token;
 }
        /// <summary>
        /// Tries to get a token for the provided audience
        /// </summary>
        /// <param name="tokenAudience">Audience to try to get a token for</param>
        /// <param name="roles">The specific roles to request access to (i.e. Group.ReadWrite.All). Optional, will use default groups assigned to clientId if not specified.</param>
        /// <returns><see cref="GenericToken"/> for the audience or NULL if unable to retrieve a token for the audience on the current connection</returns>
        internal GenericToken TryGetToken(TokenAudience tokenAudience, string[] roles = null)
        {
            GenericToken token = null;

            // Validate if we have a token already
            if (AccessTokens.ContainsKey(tokenAudience))
            {
                // We have a token already, ensure it is still valid
                token = AccessTokens[tokenAudience];

                if (token.ExpiresOn > DateTime.Now)
                {
                    // Token is still valid, ensure we dont have specific roles to check for or the requested roles to execute the command are present in the token
                    if (roles == null || roles.Length == 0 || roles.Any(r => token.Roles.Contains(r)))
                    {
                        return(token);
                    }

                    if (roles != null)
                    {
                        // Requested role was not part of the access token, throw an exception explaining which application registration is missing which role
                        throw new PSSecurityException($"Access to {tokenAudience} failed because the app registration {ClientId} in tenant {Tenant} is not granted {(roles.Length != 1 ? "any of " : string.Empty)}the permission{(roles.Length != 1 ? "s" : string.Empty)} {string.Join(", ", roles).TrimEnd(new[] { ',', ' ' })}");
                    }
                }

                // Token was no longer valid, proceed with trying to create a new token
            }

            // We do not have a token for the requested audience yet or it was no longer valid, try to create (a new) one
            switch (tokenAudience)
            {
            case TokenAudience.MicrosoftGraph:
                if (!string.IsNullOrEmpty(Tenant))
                {
                    if (Certificate != null)
                    {
                        token = GraphToken.AcquireToken(Tenant, ClientId, Certificate);
                    }
                    else if (ClientSecret != null)
                    {
                        token = GraphToken.AcquireToken(Tenant, ClientId, ClientSecret);
                    }
                }
                break;

            case TokenAudience.OfficeManagementApi:
                if (!string.IsNullOrEmpty(Tenant))
                {
                    if (Certificate != null)
                    {
                        token = OfficeManagementApiToken.AcquireToken(Tenant, ClientId, Certificate);
                    }
                    else if (ClientSecret != null)
                    {
                        token = OfficeManagementApiToken.AcquireToken(Tenant, ClientId, ClientSecret);
                    }
                }
                break;

            case TokenAudience.SharePointOnline:
                // This is not a token type we can request on demand
                return(null);
            }

            if (token != null)
            {
                // Managed to create a token for the requested audience, add it to our collection with tokens
                AccessTokens[tokenAudience] = token;
                return(token);
            }

            // Didn't have a token yet and unable to retrieve one
            return(null);
        }
        protected override void ProcessRecord()
        {
            Uri tenantUri = null;

            if (string.IsNullOrEmpty(TenantUrl) && PnPConnection.CurrentConnection != null)
            {
                HttpClient client   = new HttpClient();
                var        uri      = new Uri(PnPConnection.CurrentConnection.Url);
                var        uriParts = uri.Host.Split('.');
                if (uriParts[0].ToLower().EndsWith("-admin"))
                {
                    tenantUri =
                        new Uri(
                            $"{uri.Scheme}://{uriParts[0].ToLower().Replace("-admin", "")}.{string.Join(".", uriParts.Skip(1))}{(!uri.IsDefaultPort ? ":" + uri.Port : "")}");
                }
                else
                {
                    tenantUri = new Uri($"{uri.Scheme}://{uri.Authority}");
                }
            }
            else if (!string.IsNullOrEmpty(TenantUrl))
            {
                tenantUri = new Uri(TenantUrl);
            }
            else
            {
                throw new InvalidOperationException("Either a connection needs to be made by Connect-PnPOnline or TenantUrl and Credentials needs to be specified");
            }

            var    tenantId = Microsoft.SharePoint.Client.TenantExtensions.GetTenantIdByUrl(tenantUri.ToString());
            string password;
            string username;

            if (ParameterSpecified(nameof(Credentials)))
            {
                password = EncryptionUtility.ToInsecureString(Credentials.Password);
                username = Credentials.UserName;
            }
            else if (PnPConnection.CurrentConnection != null)
            {
                password = EncryptionUtility.ToInsecureString(PnPConnection.CurrentConnection.PSCredential.Password);
                username = PnPConnection.CurrentConnection.PSCredential.UserName;
            }
            else
            {
                throw new InvalidOperationException("Either a connection needs to be made by Connect-PnPOnline or Credentials needs to be specified");
            }

            GenericToken token = null;

            if (ParameterSpecified(nameof(Resource)) && !string.IsNullOrEmpty(Resource))
            {
                token = GenericToken.AcquireV1Token(tenantId, ClientId, username, password, Resource);
            }

            if (ParameterSpecified(nameof(Scopes)) && Scopes.Count > 0)
            {
                token = GenericToken.AcquireV2Token(tenantId, ClientId, username, password, Scopes.ToArray());
            }

            if (SetAsCurrent.IsPresent)
            {
                if (PnPConnection.CurrentConnection != null)
                {
                    if (token == null)
                    {
                        throw new InvalidOperationException($"-{nameof(SetAsCurrent)} can't be performed as no valid token could be retrieved");
                    }

                    PnPConnection.CurrentConnection.AddToken(Enums.TokenAudience.Other, token);
                }
                else
                {
                    throw new InvalidOperationException($"-{nameof(SetAsCurrent)} can only be used when having an active connection using Connect-PnPOnline");
                }
            }

            if (Decoded.IsPresent)
            {
                WriteObject(token.ParsedToken);
            }
            else
            {
                WriteObject(token.AccessToken);
            }
        }
        public virtual bool IsShotAvailable(GenericShip targetShip)
        {
            bool result = true;

            if (isDiscarded)
            {
                return(false);
            }

            if (UsesCharges && Charges == 0)
            {
                return(false);
            }

            int range;

            if (!CanShootOutsideArc)
            {
                ShotInfo shotInfo = new ShotInfo(Host, targetShip, this);
                range = shotInfo.Range;

                if (!shotInfo.IsShotAvailable)
                {
                    return(false);
                }
            }
            else
            {
                DistanceInfo distanceInfo = new DistanceInfo(Host, targetShip);
                range = distanceInfo.Range;
            }

            if (range < MinRange)
            {
                return(false);
            }
            if (range > MaxRange)
            {
                return(false);
            }

            if (RequiresTargetLockOnTargetToShoot)
            {
                List <GenericToken> waysToPay = new List <GenericToken>();

                char         letter          = Actions.GetTargetLocksLetterPair(Host, targetShip);
                GenericToken targetLockToken = Host.Tokens.GetToken(typeof(BlueTargetLockToken), letter);
                if (targetLockToken != null)
                {
                    waysToPay.Add(targetLockToken);
                }

                Host.CallOnGenerateAvailableAttackPaymentList(waysToPay);

                if (waysToPay.Count == 0)
                {
                    return(false);
                }
            }

            if (RequiresFocusToShoot)
            {
                if (!Host.Tokens.HasToken(typeof(FocusToken)))
                {
                    return(false);
                }
            }

            return(result);
        }