Esempio n. 1
0
        /// <summary>
        /// Checks whether the client is authorized.
        /// </summary>
        /// <param name="provider">Provider.</param>
        /// <param name="userId">User Id.</param>
        /// <returns>Authorization result.</returns>
        public OAuthAuthorizationResult CheckAuthorizationStatus(int userId)
        {
            User                     u     = null;
            OAuthToken               token = null;
            PropertyInfo             prop  = null;
            OAuthAuthorizationResult ret   = new OAuthAuthorizationResult()
            {
                AccessToken = string.Empty,
                Authorized  = false
            };

            if (userId > 0)
            {
                using (var repo = Resolver.Resolve <IUserRepository>())
                {
                    u = repo.Select(userId);

                    if (u != null && u.ApiAuthorization != null)
                    {
                        prop = GetClientAuthorizationProperty();

                        if (prop != null)
                        {
                            token = prop.GetValue(u.ApiAuthorization) as OAuthToken;

                            if (token != null && !string.IsNullOrEmpty(token.AccessToken) && !this.Channel.IsTokenExpiring(token))
                            {
                                ret.Authorized  = true;
                                ret.AccessToken = token.AccessToken;

                                ret = OnAuthorized(userId, ret);

                                if (ret == null || !ret.Authorized)
                                {
                                    prop.SetValue(u.ApiAuthorization, null);
                                    repo.Update(u);
                                }
                            }
                        }
                    }
                }
            }

            return(ret);
        }
Esempio n. 2
0
        /// <summary>
        /// Ensures that the user is authorized.
        /// </summary>
        /// <param name="userId">User Id.</param>
        /// <param name="forceRefresh">Value indicating whether to force refreshing the token.</param>
        /// <returns>Authorization result.</returns>
        public OAuthAuthorizationResult EnsureAuthorization(int userId, bool forceRefresh = false)
        {
            User                     u     = null;
            OAuthToken               token = null;
            PropertyInfo             prop  = null;
            OAuthAuthorizationResult ret   = new OAuthAuthorizationResult()
            {
                AccessToken = string.Empty,
                Authorized  = false
            };

            if (userId > 0)
            {
                using (var repo = Resolver.Resolve <IUserRepository>())
                {
                    u = repo.Select(userId);

                    if (u != null && u.ApiAuthorization != null)
                    {
                        prop = GetClientAuthorizationProperty();

                        if (prop != null)
                        {
                            token = prop.GetValue(u.ApiAuthorization) as OAuthToken;

                            if (token != null)
                            {
                                ret.Authorized  = !string.IsNullOrEmpty(token.RefreshToken);
                                ret.AccessToken = token.AccessToken;

                                if (this.Channel.TryRefreshAccessToken(token, forceRefresh))
                                {
                                    ret.AccessToken = token.AccessToken;
                                    prop.SetValue(u.ApiAuthorization, token);

                                    repo.Update(u);
                                }
                            }
                        }
                    }
                }
            }

            return(ret);
        }
Esempio n. 3
0
 /// <summary>
 /// Occurs when user gets authorized.
 /// </summary>
 /// <param name="userId">User Id.</param>
 /// <param name="result">Result.</param>
 /// <returns>Result.</returns>
 protected virtual OAuthAuthorizationResult OnAuthorized(int userId, OAuthAuthorizationResult result)
 {
     return(result);
 }