Пример #1
0
        private static async Task <TResult> GetClaimsAsync <TResult>(
            IAuthApplication application, IRefOptional <Authorization> authorizationRefMaybe,
            Func <IDictionary <string, string>, Guid?, bool, TResult> onClaims,
            Func <string, TResult> onFailure)
        {
            if (!authorizationRefMaybe.HasValueNotNull())
            {
                return(onClaims(new Dictionary <string, string>(), default(Guid?), false));
            }
            var authorizationRef = authorizationRefMaybe.Ref;

            return(await Api.AppSettings.ActorIdClaimType.ConfigurationString(
                       (accountIdClaimType) =>
            {
                return GetSessionAcountAsync(authorizationRef, application,
                                             (accountId, claims, authorized) =>
                {
                    var claimsWithAccountId = claims
                                              .Append(accountIdClaimType.PairWithValue(accountId.ToString()))
                                              .ToDictionary();
                    return onClaims(claimsWithAccountId, accountId, authorized);
                },
                                             (why, authorized) => onClaims(new Dictionary <string, string>(), default(Guid?), authorized));
            },
                       (why) =>
            {
                return GetSessionAcountAsync(authorizationRef, application,
                                             (accountId, claims, authorized) =>
                {
                    var claimsDictionary = claims
                                           .NullToEmpty()
                                           .ToDictionary();
                    return onClaims(claimsDictionary, accountId, authorized);
                },
                                             (why, authorized) => onClaims(new Dictionary <string, string>(), default(Guid?), authorized));
            }));
        }
Пример #2
0
        public async static Task <Session> CreateAsync(
            IAuthApplication application, IRefOptional <Authorization> authorizationRefMaybe,
            IRefOptional <Session> sessionToCreateMaybe = default)
        {
            var sessionId = sessionToCreateMaybe.HasValueNotNull() ?
                            sessionToCreateMaybe.Ref
                :
                            Ref <Session> .SecureRef();

            var session = new Session()
            {
                sessionId     = sessionId,
                refreshToken  = Security.SecureGuid.Generate().ToString("N"),
                authorization = authorizationRefMaybe,
            };

            return(await Security.AppSettings.TokenScope.ConfigurationUri(
                       scope =>
            {
                return Security.SessionServer.Configuration.AppSettings.TokenExpirationInMinutes.ConfigurationDouble(
                    async(tokenExpirationInMinutes) =>
                {
                    return await await GetClaimsAsync(application, authorizationRefMaybe,
                                                      async(claims, accountIdMaybe, authorized) =>
                    {
                        session.account = accountIdMaybe;
                        session.authorized = authorized;
                        return await await session.StorageCreateAsync(
                            (sessionIdCreated) =>
                        {
                            return Api.Auth.JwtTools.CreateToken(session.id,
                                                                 scope, TimeSpan.FromMinutes(tokenExpirationInMinutes), claims,
                                                                 (tokenNew) =>
                            {
                                session.token = tokenNew;
                                return session;
                            },
                                                                 (missingConfig) => throw new Exception(missingConfig),
                                                                 (configName, issue) => throw new Exception(issue)).AsTask();
                        },
                            onAlreadyExists: () =>
                        {
                            return sessionId.StorageUpdateAsync(
                                async(sessionToUpdate, saveAsync) =>
                            {
                                sessionToUpdate.authorization = authorizationRefMaybe;
                                sessionToUpdate.account = accountIdMaybe;
                                sessionToUpdate.authorized = authorized;
                                await saveAsync(sessionToUpdate);
                                return Api.Auth.JwtTools.CreateToken(sessionToUpdate.id,
                                                                     scope, TimeSpan.FromMinutes(tokenExpirationInMinutes), claims,
                                                                     (tokenNew) =>
                                {
                                    sessionToUpdate.token = tokenNew;
                                    return sessionToUpdate;
                                },
                                                                     (missingConfig) => throw new Exception(missingConfig),
                                                                     (configName, issue) => throw new Exception(issue));
                            });
                        });
                    },
                                                      (why) => throw new Exception(why));
                },
                    (why) => throw new Exception(why));
            },
                       (why) => throw new Exception(why)));
        }