001/**
002 * Copyright 2014 Tampere University of Technology, Pori Department
003 * 
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 * 
008 *   http://www.apache.org/licenses/LICENSE-2.0
009 * 
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package service.tut.pori.users.twitter;
017
018import core.tut.pori.http.Response;
019import core.tut.pori.http.Response.Status;
020import core.tut.pori.http.annotations.HTTPAuthenticationParameter;
021import core.tut.pori.http.annotations.HTTPMethodParameter;
022import core.tut.pori.http.annotations.HTTPService;
023import core.tut.pori.http.annotations.HTTPServiceMethod;
024import core.tut.pori.http.parameters.AuthenticationParameter;
025import core.tut.pori.http.parameters.StringParameter;
026import core.tut.pori.users.UserIdentity;
027
028/**
029 * Twitter User Service method definitions.
030 * 
031 * This class defines the APIs for registering a new account with the system and authentication using Twitter credentials, or more precisely, using the Twitter OAuth authentication. The methods also describe the means for granting the system authorization for accessing content of the Twitter account.
032 * 
033 * Creating an account or authenticating with Twitter credentials does not automatically grant the system persistent permissions for accessing the account contents.
034 * 
035 * Note that the Twitter User Service does not contain a method for removing an existing user account. An existing account can be removed using the User Service.
036 */
037@HTTPService(name=Definitions.SERVICE_USERS_TWITTER) 
038public class TwitterUserService {
039  /**
040   * This method is called by Twitter after user has either denied or granted the service permission to his/her Twitter account, and is not meant to be used directly by the end-users.
041   * 
042   * @param authenticatedUser 
043   * @param token OAuth token as provided by Twitter.
044   * @param verifier OAuth verifier generated by Twitter.
045   * @return response
046   */
047  @HTTPServiceMethod(name=Definitions.METHOD_OAUTH_LOGIN_CALLBACK)
048  public Response oAuthLoginCallback(
049      @HTTPAuthenticationParameter(required=false) AuthenticationParameter authenticatedUser,
050      @HTTPMethodParameter(name=Definitions.PARAMETER_OAUTH_TOKEN) StringParameter token,
051      @HTTPMethodParameter(name=Definitions.PARAMETER_OAUTH_VERIFIER) StringParameter verifier
052      )
053  {
054    return TwitterUserCore.processOAuthLoginCallback(authenticatedUser.getSession(), token.getValue(), verifier.getValue());
055  }
056  
057  /**
058   * This method is called by Twitter after user has either denied or granted the service permission to his/her Twitter account, and is not meant to be used directly by the end-users.
059   * 
060   * @param token OAuth token as provided by Twitter.
061   * @param verifier OAuth verifier generated by Twitter.
062   * @return response
063   */
064  @HTTPServiceMethod(name=Definitions.METHOD_OAUTH_AUTHORIZE_CALLBACK)
065  public Response oAuthAuthorizeCallback(
066      @HTTPMethodParameter(name=Definitions.PARAMETER_OAUTH_TOKEN) StringParameter token,
067      @HTTPMethodParameter(name=Definitions.PARAMETER_OAUTH_VERIFIER) StringParameter verifier
068      )
069  {
070    return TwitterUserCore.processOAuthAuthorizeCallback(token.getValue(), verifier.getValue());
071  }
072  
073  /**
074   * This method is called by Twitter after user has either denied or granted the service permission to his/her Twitter account, and is not meant to be used directly by the end-users.
075   * 
076   * @param token OAuth token as provided by Twitter.
077   * @param verifier OAuth verifier generated by Twitter.
078   * @return response
079   */
080  @HTTPServiceMethod(name=Definitions.METHOD_OAUTH_REGISTER_CALLBACK)
081  public Response oAuthRegisterCallback(
082      @HTTPMethodParameter(name=Definitions.PARAMETER_OAUTH_TOKEN) StringParameter token,
083      @HTTPMethodParameter(name=Definitions.PARAMETER_OAUTH_VERIFIER) StringParameter verifier
084      )
085  {
086    return TwitterUserCore.processOAuthRegisterCallback(token.getValue(), verifier.getValue());
087  }
088  
089  /**
090   * This method can be used to authenticate the user using Twitter credentials. The method redirects the user to Twitter's consent page, which will forward the user to the service's login OAuth callback method.
091   * 
092   * This method implements the Sign in with Twitter OAuth flow as defined by Twitter.
093   * 
094   * @param authenticatedUser
095   * @return response
096   */
097  @HTTPServiceMethod(name=Definitions.METHOD_LOGIN)
098  public Response login(@HTTPAuthenticationParameter(required=false) AuthenticationParameter authenticatedUser) {
099    if(UserIdentity.isValid(authenticatedUser.getUserIdentity())){
100      return new Response(Status.BAD_REQUEST, "Already logged in. Please logout first.");
101    }
102    return TwitterUserCore.createLoginRedirection();
103  }
104  
105  /**
106   * This method can be used to register a new user using a Twitter account. The method redirects the user to Twitter's consent page, which will forward the user to the service's register OAuth callback method. The twitter profile details will be used for the new user registration.
107   * 
108   * This method implements the Sign in with Twitter OAuth flow as defined by Twitter.
109   * 
110   * The Twitter account is assumed to be personal, and an attempt to use the same account to create multiple accounts will result in an error.
111   * 
112   * @return response
113   */
114  @HTTPServiceMethod(name=service.tut.pori.users.Definitions.METHOD_REGISTER)
115  public Response register() {
116    return TwitterUserCore.createRegisterRedirection();
117  }
118  
119  /**
120   * This method can be used to grant the service access to the authenticated user's Twitter account. The method redirects the user to Twitter's consent page, which will forward the user to the service's authorization OAuth callback method.
121   * 
122   * This method implements the Sign in with Twitter OAuth flow as defined by Twitter.
123   * 
124   * The Twitter account is assumed to be personal, and an attempt to authorize the same Twitter account to multiple users will result in an error.
125   * 
126   * @param authenticatedUser
127   * @param redirectUri The final target, where the user should be redirected to after successful authorization. If not given, a default status message will be shown.
128   * @return response
129   */
130  @HTTPServiceMethod(name=Definitions.METHOD_OAUTH_AUTHORIZATION_REDIRECT)
131  public Response authorize(
132      @HTTPAuthenticationParameter AuthenticationParameter authenticatedUser,
133      @HTTPMethodParameter(name=Definitions.PARAMETER_TWITTER_REDIRECT_URI, required=false) StringParameter redirectUri
134      )
135  {
136    return TwitterUserCore.createAuthorizationRedirection(authenticatedUser.getUserIdentity(), redirectUri.getValue());
137  }
138  
139  /**
140   * This method allows a user to revoke the previously given authorization permissions. Note that calling this method does not prevent user from authenticating (logging in) using the credentials, nor will it remove the user account from the system, but calling this method will prevent the system from accessing the user's content stored in the Twitter account. 
141   * 
142   * In practice this means, that no synchronization can be performed for the account until authorization is restored. <i>All</i> previously synchronized content will be removed from the system.
143   * 
144   * @param authenticatedUser
145   * @return response
146   */
147  @HTTPServiceMethod(name=Definitions.METHOD_UNAUTHORIZE)
148  public Response unauthorize(@HTTPAuthenticationParameter AuthenticationParameter authenticatedUser) {
149    return TwitterUserCore.removeAuthorization(authenticatedUser.getUserIdentity());
150  }
151}