186 if (denied != null) {
190 AuthenticationSessionModel authSession = null;
192 Twitter twitter =
new TwitterFactory().getInstance();
196 IdentityBrokerState idpState = IdentityBrokerState.encoded(state);
197 String clientId = idpState.getClientId();
198 String tabId = idpState.getTabId();
199 if (clientId == null || tabId == null) {
200 logger.errorf(
"Invalid state parameter: %s", state);
202 return ErrorPage.error(
session, null, Response.Status.BAD_REQUEST, Messages.INVALID_REQUEST);
206 authSession = ClientSessionCode.getClientSession(state, tabId,
session,
realm, client,
event, AuthenticationSessionModel.class);
208 String twitterToken = authSession.getAuthNote(
TWITTER_TOKEN);
211 RequestToken requestToken =
new RequestToken(twitterToken, twitterSecret);
213 AccessToken oAuthAccessToken = twitter.getOAuthAccessToken(requestToken, verifier);
214 twitter4j.User twitterUser = twitter.verifyCredentials();
216 BrokeredIdentityContext identity =
new BrokeredIdentityContext(Long.toString(twitterUser.getId()));
219 identity.setUsername(twitterUser.getScreenName());
220 identity.setName(twitterUser.getName());
223 StringBuilder tokenBuilder =
new StringBuilder();
225 tokenBuilder.append(
"{");
226 tokenBuilder.append(
"\"oauth_token\":").append(
"\"").append(oAuthAccessToken.getToken()).append(
"\"").append(
",");
227 tokenBuilder.append(
"\"oauth_token_secret\":").append(
"\"").append(oAuthAccessToken.getTokenSecret()).append(
"\"").append(
",");
228 tokenBuilder.append(
"\"screen_name\":").append(
"\"").append(oAuthAccessToken.getScreenName()).append(
"\"").append(
",");
229 tokenBuilder.append(
"\"user_id\":").append(
"\"").append(oAuthAccessToken.getUserId()).append(
"\"");
230 tokenBuilder.append(
"}");
231 String token = tokenBuilder.toString();
233 identity.setToken(token);
235 identity.getContextData().put(IdentityProvider.FEDERATED_ACCESS_TOKEN, token);
238 identity.setCode(state);
241 }
catch (WebApplicationException e) {
243 return e.getResponse();
244 }
catch (Exception e) {
245 logger.error(
"Couldn't get user profile from twitter.", e);
247 return ErrorPage.error(
session, authSession, Response.Status.BAD_GATEWAY, Messages.UNEXPECTED_ERROR_HANDLING_RESPONSE);
KeycloakSession session
Definition: TwitterIdentityProvider.java:168
ClientModel getClientByClientId(String clientId)
Response authenticated(BrokeredIdentityContext context)
static final String TWITTER_TOKENSECRET
Definition: TwitterIdentityProvider.java:73
TwitterIdentityProvider(KeycloakSession session, OAuth2IdentityProviderConfig config)
Definition: TwitterIdentityProvider.java:75
static final String TWITTER_TOKEN
Definition: TwitterIdentityProvider.java:72
AuthenticationCallback callback
Definition: TwitterIdentityProvider.java:164
C getConfig()
Definition: AbstractIdentityProvider.java:55
RealmModel realm
Definition: TwitterIdentityProvider.java:163
void sendErrorEvent()
Definition: TwitterIdentityProvider.java:251
EventBuilder event
Definition: TwitterIdentityProvider.java:165
Response cancelled(String code)
static final Logger logger
Definition: TwitterIdentityProvider.java:70