示例#1
0
        public Result <Token> GenerateToken(ITokenSpec tokenSpec)
        {
            if (tokenSpec == null)
            {
                return(Result.Fail <Token>("No token spec supplied."));
            }

            var context = createContext();

            var encoder = this.payloadEncoder ?? new PayloadEncoder();

            var signer = getSigner();

            if (!signer.IsSuccess)
            {
                return(Result.Fail <Token>(signer.Error));
            }

            var header = tokenSpec.GetHeader(context);

            return(((Result <Token>) new Token())
                   .OnSuccess(t =>
            {
                if (header != null)
                {
                    return t.AppendPart(encoder.Encode(header));
                }
                else
                {
                    return t;
                }
            })
                   .OnSuccess(t =>
            {
                var allClaimsInBuilder =
                    this.payload.Keys.Concat(this.automaticClaims);

                var filteredClaims = tokenSpec.FilterClaims(
                    claims: allClaimsInBuilder,
                    context: context);

                var compiledClaims = this.payload
                                     .Where(kvp => filteredClaims.Contains(kvp.Key))
                                     .Concat(this.deriveAutomaticClaims(
                                                 tokenSpec: tokenSpec,
                                                 automaticClaims: this.automaticClaims.Intersect(filteredClaims),
                                                 context: context))
                                     .ToDictionary(kvp => kvp.Key, kvp => kvp.Value);

                return t.AppendPart(
                    tokenPart: encoder.Encode(compiledClaims));
            })
                   .OnSuccess(t =>
            {
                return t.AppendPart(
                    tokenPart: signer.Value.SignToken(t.ToString()));
            }));
        }
示例#2
0
        public T GetClaimValue <T>(string key, ITokenSpec tokenSpec)
        {
            T value = default(T);

            if (this.automaticClaims.Contains(key))
            {
                var context = this.createContext();

                var result = tokenSpec
                             .GetAutomaticValueForClaim(key, context)
                             .ThrowOnFail();

                value = (T)result.Value;
            }
            else if (this.payload.Keys.Contains(key))
            {
                value = (T)this.payload[key];
            }

            return(value);
        }
示例#3
0
        private IDictionary <string, object> deriveAutomaticClaims(
            ITokenSpec tokenSpec,
            IEnumerable <string> automaticClaims,
            TokenBuilderContext context)
        {
            Dictionary <string, object> answer = new Dictionary <string, object>();

            foreach (var claim in automaticClaims)
            {
                var result = tokenSpec
                             .GetAutomaticValueForClaim(claim, context)
                             .ThrowOnFail();

                if (result.Value != null)
                {
                    answer[claim] = result.Value;
                }
            }

            return(answer);
        }