/** * Configure this builder with an existing {@link org.agorava.api.oauth.OAuthSession}. Convenient to clone a session * or to create an unproxified version of a session * * @param session the session to read values from * @return this builder */ public Builder readFromOAuthSession(OAuthSession session) { qualifier(session.getServiceQualifier()) .requestToken(session.getRequestToken()) .accessToken(session.getAccessToken()) .verifier(session.getVerifier()) .userProfile(session.getUserProfile()) .repo(session.getRepo()) .id(session.getId()); return this; }
/** * @return true if the session is the {@link #NULL} OAuthSession */ public boolean isNull() { return this.equals(NULL); }
@Override public String toString() { return getServiceName() + " - " + (isConnected() ? getUserProfile().getFullName() : "not connected"); }
@Override public synchronized void endDance() { OAuthSession currentSession = getCurrentSession(); System.out.println("Verifier ("+ getVerifierParamName() + ") : " + currentSession.getVerifier()); // FIXME replace with logger or disable if (currentSession.getAccessToken() == null) { currentSession.setAccessToken(getCurrentService().getAccessToken(currentSession.getRequestToken(), currentSession.getVerifier())); } if (currentSession.getAccessToken() != null) { currentSession.setRequestToken(null); currentSession.setUserProfile(getCurrentUserProfileService().getUserProfile()); currentSession.getRepo().add(currentSession); completeEvt.select(currentSession.getServiceQualifier()).fire(new OAuthComplete(SocialEvent.Status.SUCCESS, "", currentSession)); //TODO: reactivate logger } else { // FIXME Launch an exception !! } }
@Override public void authenticate() { if (session.isConnected()) { UserProfile userProfile = session.getUserProfile(); credentials.setCredential(session.getAccessToken()); setStatus(AuthenticationStatus.SUCCESS); setAccount(new AgoravaUser(userProfile)); } else { String authorizationUrl = lifeCycleService.startDanceFor(settings.getQualifier()); try { response.get().sendRedirect(authorizationUrl); } catch (IOException e) { throw new AgoravaException("Unable to redirect user to: " + authorizationUrl); } credentials.setStatus(Status.IN_PROGRESS); setStatus(AuthenticationStatus.DEFERRED); } } }
@Override public OAuthSession resolveSessionForQualifier(Annotation qualifier) { OAuthSession current = getCurrentSession(); if (current.getServiceQualifier().equals(qualifier)) { return current; } if (getCurrentRepository().getCurrent().equals(OAuthSession.NULL)) { buildSessionFor(qualifier); } else if (!getCurrentRepository().getCurrent().getServiceQualifier().equals(qualifier)) { throw new ProviderMismatchException("Inconsistent state between repo and service. In repo Session provider is " + getCurrentRepository().getCurrent().getServiceName() + " while service provider is " + qualifier); } return getCurrentRepository().getCurrent(); }
/** * @return the name of the service */ public String getServiceName() { return AgoravaContext.getQualifierToService().get(getServiceQualifier()); }
@Override public String getAsString(FacesContext context, UIComponent component, Object value) { return ((OAuthSession) value).getId(); } }
@Override public Token getAccessToken() { OAuthSession session = getSession(); return session.getAccessToken(); }
@Override public String getScreenName() { return getSession().getUserProfile().getFullName(); }
@Override public String getVerifier() { OAuthSession session = getSession(); final String verifier = session.getVerifier(); LOGGER.fine("V: " + verifier); // FIXME change to finest or comment out return verifier; }
@Override public boolean isConnected() { return getSession().isConnected(); }
protected void renderResponse(HttpServletRequest req, HttpServletResponse resp) { String internalCallBack = (String) lifeCycleService.getCurrentSession().getExtraData().get(AgoravaConstants .INTERN_CALLBACK_PARAM); if (internalCallBack == null) internalCallBack = AgoravaContext.getInternalCallBack(); try { if (internalCallBack.startsWith("/")) internalCallBack = req.getContextPath() + internalCallBack; internalCallBack = resp.encodeRedirectURL(internalCallBack + "?" + req.getQueryString()); resp.sendRedirect(internalCallBack); } catch (IOException e) { throw new AgoravaException(e); } }
@PostConstruct protected void init() { UserSessionRepository r; OAuthSession s; s = globalRepository.getOauthSession(getSessionId()); if (s != null) { currentSession = s; currentRepo = s.getRepo(); } else { r = globalRepository.get(getRepoId()); if (r != null) { currentRepo = r; } else { currentRepo = globalRepository.createNew(); } currentSession = currentRepo.getCurrent(); } }
@Override public OAuthService getCurrentService() { return services.select(getCurrentSession().getServiceQualifier()).get(); }
@Override public OAuthSession get(String id) { for (OAuthSession session : activeSessions) { if (id.equals(session.getId())) return session; } return null; }
@Override public String getProfileId() { return getSession().getUserProfile().getId(); }
@Override public String startDanceFor(String providerName, String internalCallBack) { OAuthSession session = buildSessionFor(providerName); if (internalCallBack != null && !"".equals(internalCallBack.trim())) { session.getExtraData().put(AgoravaConstants.INTERN_CALLBACK_PARAM, internalCallBack); } return getCurrentService().getAuthorizationUrl(); }
@Override public UserProfileService getCurrentUserProfileService() { return userProfileServices.select(getCurrentSession().getServiceQualifier()).get(); }
@Override public OAuthSession getOauthSession(String sid) { if (sid != null && !sid.isEmpty()) { for (OAuthSession session : getAllOauthSessions()) { if (session.getId().equals(sid)) { return session; } } } return null; }