Ejemplo n.º 1
0
        public User LoginWithTwitter(TwitterCredential credentials)
        {
            if (credentials == null)
                throw new ParamMissingException("Missing credentials.");

            User user = Repository.Single<User>(c => c.Deleted == false
                && c.Twitter.TwitterId.Equals(credentials.TwitterId)
                , "Credential", "Twitter");
            if (user == null)
                return null;
            else
            {
                // Regenrate the security token.
                user.Credential.SecurityToken = Guid.NewGuid().ToString();
                user.Credential.LastModified = DateTime.UtcNow;
                user.Credential.TokenExpiry = DateTime.MaxValue;
                user.Twitter.TwitterAccessToken = credentials.TwitterAccessToken;
                user.Twitter.TwitterSecret = credentials.TwitterSecret;
                Repository.Save();
                return user;
            }
        }
Ejemplo n.º 2
0
        public HttpResponseMessage LoginWithTwitter(TwitterCredential userCred)
        {
            try
            {
                if (userCred == null
                    || ExtensionMethods.IsEmptyOrSpace(userCred.TwitterId)
                    || ExtensionMethods.IsEmptyOrSpace(userCred.TwitterAccessToken)
                    || ExtensionMethods.IsEmptyOrSpace(userCred.TwitterSecret))
                    return Request.CreateResponse(HttpStatusCode.NotAcceptable, GetErrorResponse("Missing required param"));

                User user = _userManager.LoginWithTwitter(userCred);
                if (user == null)
                    return Request.CreateResponse(HttpStatusCode.Unauthorized, GetErrorResponse("Login failed."));
                else
                {
                    UserLite lite = new UserLite();
                    lite.CopyFromUserIncludeCredentials(user);

                    UserCreationResponse result = new UserCreationResponse
                    {
                        UserId = user.UserId,
                        Token = user.Credential.SecurityToken,
                        User = lite
                    };
                    return Request.CreateResponse(HttpStatusCode.OK, result);
                }
            }
            catch (ParamMissingException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
            } catch (AlreadyExistsException e){
                return Request.CreateResponse(HttpStatusCode.Conflict, new ErrorResponse{ Message = e.Message });
            }catch (InvalidValueException e){
                return Request.CreateResponse(HttpStatusCode.NotAcceptable, new ErrorResponse{ Message = e.Message });
            }catch (UserNotFoundException e){
                return Request.CreateResponse(HttpStatusCode.NotFound, new ErrorResponse{ Message = e.Message });
            }catch (Exception e){
                return Request.CreateResponse(HttpStatusCode.InternalServerError, new ErrorResponse { Message = "Oops, server encountered an issue... " + e.Message });
            }
        }