Exemple #1
0
 public static string GetClaimsChallenge(HttpResponseMessage response)
 {
     return(ParseWwwAuthenticate(response)?
            .Where((p) => string.Equals(p.Item1, "claims", StringComparison.OrdinalIgnoreCase))
            .Select(p => Base64UrlHelper.DecodeToString(p.Item2))
            .FirstOrDefault());
 }
Exemple #2
0
        public AcrToken(string token)
        {
            _token = token;
            string decodedToken    = Base64UrlHelper.DecodeToString(_token.Split('.')[1]);
            int    unixTimeSeconds = JsonDocument.Parse(decodedToken)
                                     .RootElement
                                     .EnumerateObject()
                                     .Where(p => p.Name == "exp")
                                     .Select(p => p.Value.GetInt32())
                                     .First();

            _exp = DateTimeOffset.FromUnixTimeSeconds(unixTimeSeconds).UtcDateTime;
        }
Exemple #3
0
        public JweDecode(string compact_jwe)
        {
            string[] parts = compact_jwe.Split('.');

            if (parts.Length != 5)
            {
                throw new Exception("Malformed input");
            }

            encoded_header = parts[0];
            string header = Base64UrlHelper.DecodeToString(encoded_header);

            encrypted_key = Base64UrlHelper.DecodeToBytes(parts[1]);
            init_vector   = Base64UrlHelper.DecodeToBytes(parts[2]);
            ciphertext    = Base64UrlHelper.DecodeToBytes(parts[3]);
            auth_tag      = Base64UrlHelper.DecodeToBytes(parts[4]);

            protected_header = JsonConvert.DeserializeObject <JWE_header>(header);
        }
Exemple #4
0
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            string resourceUrlOrId;

            if (ParameterSetName == KnownResourceNameParameterSet)
            {
                if (ResourceTypeName == null)
                {
                    ResourceTypeName = SupportedResourceNames.Arm;
                }
                if (!SupportedResourceNames.ResourceNameMap.ContainsKey(ResourceTypeName))
                {
                    throw new ArgumentException(Properties.Resources.InvalidResourceTypeName.FormatInvariant(ResourceTypeName), nameof(ResourceTypeName));
                }
                resourceUrlOrId = SupportedResourceNames.ResourceNameMap[ResourceTypeName];
            }
            else
            {
                resourceUrlOrId = ResourceUrl;
            }

            IAzureContext context = DefaultContext;

            if (TenantId == null)
            {
                TenantId = context.Tenant?.Id;
            }

            IAccessToken accessToken = AzureSession.Instance.AuthenticationFactory.Authenticate(
                context.Account,
                context.Environment,
                TenantId,
                null,
                ShowDialog.Never,
                null,
                null,
                resourceUrlOrId);

            var result = new PSAccessToken()
            {
                Token    = accessToken.AccessToken,
                TenantId = TenantId,
                UserId   = accessToken.UserId,
            };

            result.ExpiresOn = (accessToken as MsalAccessToken)?.ExpiresOn ?? result.ExpiresOn;
            if (result.ExpiresOn == default(DateTimeOffset))
            {
                try
                {
                    var tokenParts   = accessToken.AccessToken.Split('.');
                    var decodedToken = Base64UrlHelper.DecodeToString(tokenParts[1]);

                    var tokenDocument = JsonDocument.Parse(decodedToken);
                    int expSeconds    = tokenDocument.RootElement.EnumerateObject()
                                        .Where(p => p.Name == "exp")
                                        .Select(p => p.Value.GetInt32())
                                        .First();
                    result.ExpiresOn = UnixEpoch.AddSeconds(expSeconds);
                }
                catch (Exception e)//Ignore exception if fails to parse exp from token
                {
                    WriteDebug("Unable to parse exp in token: " + e.ToString());
                }
            }

            WriteObject(result);
        }