Merge pull request #956 from pedroigor/master

[KEYCLOAK-992] - Storing twitter tokens.
This commit is contained in:
Pedro Igor 2015-02-09 21:31:56 -02:00
commit e84ae5e68d

View file

@ -31,8 +31,10 @@ import org.keycloak.models.FederatedIdentityModel;
import org.keycloak.social.SocialIdentityProvider; import org.keycloak.social.SocialIdentityProvider;
import twitter4j.Twitter; import twitter4j.Twitter;
import twitter4j.TwitterFactory; import twitter4j.TwitterFactory;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken; import twitter4j.auth.RequestToken;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap; import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response; import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
@ -98,15 +100,26 @@ public class TwitterIdentityProvider extends AbstractIdentityProvider<OAuth2Iden
RequestToken requestToken = new RequestToken(twitterToken, twitterSecret); RequestToken requestToken = new RequestToken(twitterToken, twitterSecret);
twitter.getOAuthAccessToken(requestToken, verifier); AccessToken oAuthAccessToken = twitter.getOAuthAccessToken(requestToken, verifier);
twitter4j.User twitterUser = twitter.verifyCredentials(); twitter4j.User twitterUser = twitter.verifyCredentials();
FederatedIdentity user = new FederatedIdentity(Long.toString(twitterUser.getId())); FederatedIdentity identity = new FederatedIdentity(Long.toString(twitterUser.getId()));
user.setUsername(twitterUser.getScreenName()); identity.setUsername(twitterUser.getScreenName());
user.setName(twitterUser.getName()); identity.setName(twitterUser.getName());
return AuthenticationResponse.end(user); StringBuilder tokenBuilder = new StringBuilder();
tokenBuilder.append("{");
tokenBuilder.append("\"oauth_token\":").append("\"").append(oAuthAccessToken.getToken()).append("\"").append(",");
tokenBuilder.append("\"oauth_token_secret\":").append("\"").append(oAuthAccessToken.getTokenSecret()).append("\"").append(",");
tokenBuilder.append("\"screen_name\":").append("\"").append(oAuthAccessToken.getScreenName()).append("\"").append(",");
tokenBuilder.append("\"user_id\":").append("\"").append(oAuthAccessToken.getUserId()).append("\"");
tokenBuilder.append("}");
identity.setToken(tokenBuilder.toString());
return AuthenticationResponse.end(identity);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -114,6 +127,6 @@ public class TwitterIdentityProvider extends AbstractIdentityProvider<OAuth2Iden
@Override @Override
public Response retrieveToken(FederatedIdentityModel identity) { public Response retrieveToken(FederatedIdentityModel identity) {
return Response.noContent().build(); return Response.ok(identity.getToken()).type(MediaType.APPLICATION_JSON).build();
} }
} }