Пример #1
0
        /// <summary>
        /// Instantiates a new instance of <see cref="WampCraUserDbSessionAuthenticator"/>
        /// given the WAMP-CRA user details, the <see cref="WampAuthenticationRole"/> and
        /// the user's session id.
        /// </summary>
        /// <param name="user">The given user's WAMP-CRA details, used for authentication.</param>
        /// <param name="role">The given user's role, used for authorization.</param>
        /// <param name="sessionId">The given user's session id.</param>
        public WampCraUserDbSessionAuthenticator(WampCraUser user, WampAuthenticationRole role, long sessionId) :
            base(user.AuthenticationId)
        {
            mUser = user;

            if (user.Salt != null)
            {
                CraChallengeDetails =
                    new WampCraChallengeDetails(user.Salt,
                                                user.Iterations,
                                                user.KeyLength);
            }

            WelcomeDetails = new WelcomeDetails
            {
                AuthenticationRole     = role.AuthenticationRole,
                AuthenticationProvider = role.AuthenticationProvider,
            };

            WampCraPendingClientDetails details =
                new WampCraPendingClientDetails()
            {
                AuthenticationId       = user.AuthenticationId,
                SessionId              = sessionId,
                AuthenticationProvider = role.AuthenticationProvider,
                AuthenticationRole     = role.AuthenticationRole
            };

            Authorizer = role.Authorizer;

            mAuthenticationChallenge = details.ToString();
        }
        public IWampSessionAuthenticator GetSessionAuthenticator
            (WampPendingClientDetails details,
            IWampSessionAuthenticator transportAuthenticator)
        {
            HelloDetails helloDetails = details.HelloDetails;

            if ((helloDetails.AuthenticationMethods == null) ||
                !helloDetails.AuthenticationMethods.Contains(WampCra))
            {
                throw new WampAuthenticationException("supports only 'wampcra' authentication");
            }

            WampCraUser user =
                mUserDb.GetUserById(helloDetails.AuthenticationId);

            if (user == null)
            {
                throw new WampAuthenticationException
                          (string.Format("no user with authid '{0}' in user database",
                                         helloDetails.AuthenticationId));
            }

            user.AuthenticationId = user.AuthenticationId ??
                                    helloDetails.AuthenticationId;

            string authenticationRole = user.AuthenticationRole;

            WampAuthenticationRole role =
                mAuthenticationProvider.GetRoleByName(details.Realm, authenticationRole);

            if (role == null)
            {
                throw new WampAuthenticationException
                          (message: string.Format("authentication failed - realm '{0}' has no role '{1}'",
                                                  details.Realm,
                                                  authenticationRole),
                          reason: WampErrors.NoSuchRole);
            }

            role.AuthenticationRole = role.AuthenticationRole ??
                                      authenticationRole;

            role.AuthenticationProvider = role.AuthenticationProvider ??
                                          mAuthenticationProvider.ProviderName;

            return(new WampCraUserDbSessionAuthenticator(user, role, details.SessionId));
        }