Ejemplo n.º 1
0
        protected virtual string _AddSalt(string token, TokenSalt salt)
        {
            token = _Replace(token, 2, salt.VersionPosition.ToString());

            // add version
            token = _SetVersion(token, salt.VersionPosition);

            // add salt
            token = _Replace(token, salt.VersionPosition + 1, salt.SaltPosition.ToString("D2"));
            token = _Replace(token, salt.SaltPosition, salt.Salt);

            // add data and timestamp
            token = _Replace(token, salt.VersionPosition + 5, salt.DataLength.ToString("D2"));
            token = _Replace(token, salt.VersionPosition + 3, salt.DataPosition.ToString("D2"));

            if (!string.IsNullOrEmpty(salt.Data))
            {
                if (salt.Data.Length > _settings.DataMaxlength - _TIMESTAMP_LENGTH)
                {
                    var message = $"data length is not in range, max length is {_settings.DataMaxlength - _TIMESTAMP_LENGTH}";

                    throw new ArgumentOutOfRangeException(nameof(salt.Data), message);
                }

                token = _Replace(token, salt.DataPosition, salt.Data);
            }

            token = _Replace(token, salt.DataPosition + _settings.DataMaxlength - _TIMESTAMP_LENGTH, salt.Timestamp.ToString("x8"));

            // apply backpack
            return(_backpack.Encode(token));
        }
        public AuthenticationModule(
            ICommandSender commandSender,
            ITokenEncoder tokenEncoder,
            IQueryProcessor queryProcessor)
        {
            Post("/api/register", async _ => {
                var command = this.Bind <RegisterUserCommand>();
                await commandSender.Send(command);
                var token = new JwtPayload(command.UserId);
                return(Negotiate
                       .WithStatusCode(HttpStatusCode.OK)
                       .WithModel(new {
                    token = tokenEncoder.Encode(token),
                    expire = token.Expire
                }));
            });

            Post("/api/login", async _ => {
                var userId = this.Request.Form.UserId;
                if (string.IsNullOrEmpty(userId))
                {
                    return(Negotiate.WithStatusCode(HttpStatusCode.Unauthorized));
                }
                var user = await queryProcessor.Query(new FindUserQuery(userId));
                if (user == null)
                {
                    return(Negotiate.WithStatusCode(HttpStatusCode.Unauthorized));
                }
                var token = new JwtPayload(userId);
                return(Negotiate
                       .WithStatusCode(HttpStatusCode.OK)
                       .WithModel(new {
                    token = tokenEncoder.Encode(token),
                    expire = token.Expire
                }));
            });
        }
Ejemplo n.º 3
0
        protected void DescribeAutoTokens(DescribeFor describe)
        {
            foreach (var part in _contentManager.Query <TPart>().List())
            {
                var titlePart = part.As <ITitleAspect>();
                if (titlePart == null)
                {
                    continue;
                }

                var encodingContext = _tokenEncoder.Encode("*", titlePart.Title);
                describe.Token(
                    encodingContext.Token,
                    T(encodingContext.Token.Replace("*", "<value>")),
                    T("A value (as String) from the {0} {1}'s InfoSet", titlePart.Title, typeof(TPart).Name)
                    );
            }
        }