Ejemplo n.º 1
0
        internal string DeserializeAuthToken(string token)
        {
            var principle = JwtHelper.GetClaimsPrincipal(token);

            if (principle == null)
            {
                return(null);
            }
            var identity = principle.Identity as ClaimsIdentity;

            if (identity == null)
            {
                return(null);
            }
            if (!identity.IsAuthenticated)
            {
                return(null);
            }
            var authKey  = identity.FindFirst("auth_key").Value;
            var authHash = identity.FindFirst("auth_hash").Value;

            if (string.Format("{0}:{1}", authKey, authSecret).Sha256() == authHash)
            {
                return(authKey);
            }
            return(null);
        }
Ejemplo n.º 2
0
        private async Task <ReturnDto> Auth(VerifyEnum authority, AuthorityModel model)
        {
            ReturnDto ret;
            var       authorities = _issuers["owner"].Authorities;
            string    token       = model.token;

            if (string.IsNullOrWhiteSpace(token))
            {
                token = JwtHelper.GenerateToken(new Claim[] { }, 60);
            }

            var principle = JwtHelper.GetClaimsPrincipal(token);

            if (principle?.Identity?.IsAuthenticated == true)
            {
                try {
                    var claimsIdentity = principle.Identity as ClaimsIdentity;
                    var verifyResult   = _issuers["owner"].Verify(authority, claimsIdentity.Claims.ToArray(), model.payload);

                    ret = await ResultFactory(authority, verifyResult);

                    return(ret);
                } catch (Exception exc) {
                    ret = ExceptionReturn(exc);
                    return(ret);
                }
            }
            return(TokenNotValid());
        }
Ejemplo n.º 3
0
        public async Task <IActionResult> Account(string authority, [FromBody] AuthorityModel model)
        {
            if (model == null || model?.payload == null)
            {
                return(Unauthorized());
            }

            var authorities = _issuers["owner"].Authorities;

            if (!authorities.Any())
            {
                return(Unauthorized());
            }

            string token = model.token;

            if (string.IsNullOrWhiteSpace(authority))
            {
                authority = authorities.Keys.ToArray()[0];
                token     = JwtHelper.GenerateToken(new Claim[] { }, _settings.Value.AwronoreSettings.OtpTimeOut);
            }

            if (string.IsNullOrWhiteSpace(token))
            {
                return(Unauthorized());
            }

            var principle = JwtHelper.GetClaimsPrincipal(token);

            if (principle?.Identity?.IsAuthenticated == true)
            {
                try
                {
                    var claimsIdentity = principle.Identity as ClaimsIdentity;
                    var(verifyResult, otp) = await _issuers["owner"].VerifyAsync(authority, claimsIdentity.Claims.ToArray(), model.payload);
                    if (verifyResult.Authority == null)
                    {
                        return(Ok(new { auth_token = verifyResult.Token }));
                    }
                    return(Ok(new
                    {
                        verify_token = verifyResult.Token,
                        authority = verifyResult.Authority,
                        parameters = verifyResult.Payload,
                        otp = "",
                        timeOut = _settings.Value.AwronoreSettings.OtpTimeOut
                    }));
                }
                catch (Exception e)
                {
                    if (e is AwronoreIdentityException)
                    {
                        throw e;
                    }

                    return(Unauthorized());
                }
            }
            return(Unauthorized());
        }
Ejemplo n.º 4
0
        private long GetUserIdFromAuthToken(string encriptToken)
        {
            var auth_token = StringCipher.Decrypt(encriptToken);
            var principle  = JwtHelper.GetClaimsPrincipal(auth_token);

            if (principle?.Identity?.IsAuthenticated == true)
            {
                var claimsIdentity = principle.Identity as ClaimsIdentity;
                var userId         = claimsIdentity.Claims.FirstOrDefault(x => x.Type == "userId").Value;
                if (string.IsNullOrWhiteSpace(userId))
                {
                    return(default(long));
                }
                return(long.Parse(userId));
            }
            return(default(long));
        }
Ejemplo n.º 5
0
        public IActionResult Account(string authority, [FromBody] AuthorityModel model)
        {
            if (model == null || model?.payload == null)
            {
                return(Unauthorized());
            }
            var authorities = _issuers["owner"].Authorities;

            if (!authorities.Any())
            {
                return(Unauthorized());
            }
            string token = model.token;

            if (string.IsNullOrWhiteSpace(authority))
            {
                authority = authorities.Keys.ToArray()[0];
                token     = JwtHelper.GenerateToken(new Claim[] { }, 60);
            }
            if (string.IsNullOrWhiteSpace(token))
            {
                return(Unauthorized());
            }

            var principle = JwtHelper.GetClaimsPrincipal(token);

            if (principle?.Identity?.IsAuthenticated == true)
            {
                try
                {
                    var claimsIdentity = principle.Identity as ClaimsIdentity;
                    var verifyResult   = _issuers["owner"].Verify(authority, claimsIdentity.Claims.ToArray(), model.payload);
                    if (verifyResult.Authority == null)
                    {
                        return(Ok(new { auth_token = verifyResult.Token }));
                    }
                    return(Ok(new { verify_token = verifyResult.Token, authority = verifyResult.Authority, parameters = verifyResult.Payload }));
                }
                catch
                {
                    return(Unauthorized());
                }
            }
            return(Unauthorized());
        }