Search in sources :

Example 1 with OAuthUser

use of org.olat.login.oauth.model.OAuthUser in project OpenOLAT by OpenOLAT.

the class OAuthRegistrationController method formOK.

@Override
protected void formOK(UserRequest ureq) {
    String lang = langEl.getSelectedKey();
    String username = usernameEl.getValue();
    OAuthUser oauthUser = registration.getOauthUser();
    User newUser = userManager.createUser(null, null, null);
    for (UserPropertyHandler userPropertyHandler : userPropertyHandlers) {
        FormItem propertyItem = this.flc.getFormComponent(userPropertyHandler.getName());
        userPropertyHandler.updateUserFromFormItem(newUser, propertyItem);
    }
    // Init preferences
    newUser.getPreferences().setLanguage(lang);
    newUser.getPreferences().setInformSessionTimeout(true);
    String id;
    if (StringHelper.containsNonWhitespace(oauthUser.getId())) {
        id = oauthUser.getId();
    } else if (StringHelper.containsNonWhitespace(oauthUser.getEmail())) {
        id = oauthUser.getEmail();
    } else {
        id = username;
    }
    authenticatedIdentity = securityManager.createAndPersistIdentityAndUser(username, null, newUser, registration.getAuthProvider(), id, null);
    // Add user to system users group
    SecurityGroup olatuserGroup = securityManager.findSecurityGroupByName(Constants.GROUP_OLATUSERS);
    securityManager.addIdentityToSecurityGroup(authenticatedIdentity, olatuserGroup);
    // open disclaimer
    removeAsListenerAndDispose(disclaimerController);
    disclaimerController = new DisclaimerController(ureq, getWindowControl());
    listenTo(disclaimerController);
    cmc = new CloseableModalController(getWindowControl(), translate("close"), disclaimerController.getInitialComponent(), true, translate("disclaimer.title"));
    cmc.activate();
    listenTo(cmc);
}
Also used : OAuthUser(org.olat.login.oauth.model.OAuthUser) User(org.olat.core.id.User) DisclaimerController(org.olat.registration.DisclaimerController) OAuthUser(org.olat.login.oauth.model.OAuthUser) FormItem(org.olat.core.gui.components.form.flexible.FormItem) CloseableModalController(org.olat.core.gui.control.generic.closablewrapper.CloseableModalController) SecurityGroup(org.olat.basesecurity.SecurityGroup) UserPropertyHandler(org.olat.user.propertyhandlers.UserPropertyHandler)

Example 2 with OAuthUser

use of org.olat.login.oauth.model.OAuthUser in project OpenOLAT by OpenOLAT.

the class Google2Provider method parseInfos.

public OAuthUser parseInfos(String body) {
    OAuthUser user = new OAuthUser();
    try {
        JSONObject obj = new JSONObject(body);
        user.setId(getValue(obj, "id"));
        user.setFirstName(getValue(obj, "given_name"));
        user.setLastName(getValue(obj, "family_name"));
        user.setLang(getValue(obj, "locale"));
    } catch (JSONException e) {
        log.error("", e);
    }
    return user;
}
Also used : JSONObject(org.json.JSONObject) OAuthUser(org.olat.login.oauth.model.OAuthUser) JSONException(org.json.JSONException)

Example 3 with OAuthUser

use of org.olat.login.oauth.model.OAuthUser in project OpenOLAT by OpenOLAT.

the class OpenIdConnectFullConfigurableProvider method parseInfos.

public OAuthUser parseInfos(String body) {
    OAuthUser user = new OAuthUser();
    try {
        JSONObject obj = new JSONObject(body);
        user.setId(getValue(obj, "sub"));
        user.setEmail(getValue(obj, "sub"));
    } catch (JSONException e) {
        log.error("", e);
    }
    return user;
}
Also used : JSONObject(org.json.JSONObject) OAuthUser(org.olat.login.oauth.model.OAuthUser) JSONException(org.json.JSONException)

Example 4 with OAuthUser

use of org.olat.login.oauth.model.OAuthUser in project OpenOLAT by OpenOLAT.

the class OAuthDispatcher method execute.

@Override
public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String uri = request.getRequestURI();
    try {
        uri = URLDecoder.decode(uri, "UTF-8");
    } catch (UnsupportedEncodingException e) {
        throw new AssertException("UTF-8 encoding not supported!!!!");
    }
    String uriPrefix = DispatcherModule.getLegacyUriPrefix(request);
    uri = uri.substring(uriPrefix.length());
    UserRequest ureq = null;
    try {
        // upon creation URL is checked for
        ureq = new UserRequestImpl(uriPrefix, request, response);
    } catch (NumberFormatException nfe) {
        if (log.isDebug()) {
            log.debug("Bad Request " + request.getPathInfo());
        }
        DispatcherModule.sendBadRequest(request.getPathInfo(), response);
        return;
    }
    String error = request.getParameter("error");
    if (null != error) {
        error(ureq, translateOauthError(ureq, error));
        return;
    }
    String problem = request.getParameter("oauth_problem");
    if (problem != null && "token_rejected".equals(problem.trim())) {
        error(ureq, translateOauthError(ureq, error));
        return;
    }
    try {
        HttpSession sess = request.getSession();
        // OAuth 2.0 hasn't any request token
        Token requestToken = (Token) sess.getAttribute(OAuthConstants.REQUEST_TOKEN);
        OAuthService service = (OAuthService) sess.getAttribute(OAuthConstants.OAUTH_SERVICE);
        OAuthSPI provider = (OAuthSPI) sess.getAttribute(OAuthConstants.OAUTH_SPI);
        Token accessToken;
        if (provider == null) {
            log.audit("OAuth Login failed, no provider in request");
            DispatcherModule.redirectToDefaultDispatcher(response);
            return;
        } else if (provider.isImplicitWorkflow()) {
            String idToken = ureq.getParameter("id_token");
            if (idToken == null) {
                redirectImplicitWorkflow(ureq);
                return;
            } else {
                Verifier verifier = OpenIDVerifier.create(ureq, sess);
                accessToken = service.getAccessToken(requestToken, verifier);
            }
        } else {
            String requestVerifier = request.getParameter("oauth_verifier");
            if (requestVerifier == null) {
                // OAuth 2.0 as a code
                requestVerifier = request.getParameter("code");
            }
            accessToken = service.getAccessToken(requestToken, new Verifier(requestVerifier));
        }
        OAuthUser infos = provider.getUser(service, accessToken);
        if (infos == null || !StringHelper.containsNonWhitespace(infos.getId())) {
            error(ureq, translate(ureq, "error.no.id"));
            log.error("OAuth Login failed, no infos extracted from access token: " + accessToken);
            return;
        }
        OAuthRegistration registration = new OAuthRegistration(provider.getProviderName(), infos);
        login(infos, registration);
        if (provider instanceof OAuthUserCreator) {
            Identity newIdentity;
            OAuthUserCreator userCreator = (OAuthUserCreator) provider;
            if (registration.getIdentity() == null) {
                newIdentity = userCreator.createUser(infos);
            } else {
                newIdentity = userCreator.updateUser(infos, registration.getIdentity());
            }
            if (newIdentity != null) {
                registration.setIdentity(newIdentity);
            }
        }
        if (registration.getIdentity() == null) {
            if (CoreSpringFactory.getImpl(OAuthLoginModule.class).isAllowUserCreation()) {
                register(request, response, registration);
            } else {
                error(ureq, translate(ureq, "error.account.creation"));
                log.error("OAuth Login ok but the user has not an account on OpenOLAT: " + infos);
            }
        } else {
            if (ureq.getUserSession() != null) {
                // re-init the activity logger
                ThreadLocalUserActivityLoggerInstaller.initUserActivityLogger(request);
            }
            Identity identity = registration.getIdentity();
            int loginStatus = AuthHelper.doLogin(identity, provider.getProviderName(), ureq);
            if (loginStatus != AuthHelper.LOGIN_OK) {
                if (loginStatus == AuthHelper.LOGIN_NOTAVAILABLE) {
                    DispatcherModule.redirectToServiceNotAvailable(response);
                } else {
                    // error, redirect to login screen
                    DispatcherModule.redirectToDefaultDispatcher(response);
                }
            } else {
                // update last login date and register active user
                UserDeletionManager.getInstance().setIdentityAsActiv(identity);
                MediaResource mr = ureq.getDispatchResult().getResultingMediaResource();
                if (mr instanceof RedirectMediaResource) {
                    RedirectMediaResource rmr = (RedirectMediaResource) mr;
                    rmr.prepare(response);
                } else {
                    // error, redirect to login screen
                    DispatcherModule.redirectToDefaultDispatcher(response);
                }
            }
        }
    } catch (Exception e) {
        log.error("Unexpected error", e);
        error(ureq, translate(ureq, "error.generic"));
    }
}
Also used : AssertException(org.olat.core.logging.AssertException) HttpSession(javax.servlet.http.HttpSession) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Token(org.scribe.model.Token) OpenIDVerifier(org.olat.login.oauth.spi.OpenIDVerifier) Verifier(org.scribe.model.Verifier) AssertException(org.olat.core.logging.AssertException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OAuthService(org.scribe.oauth.OAuthService) OAuthUser(org.olat.login.oauth.model.OAuthUser) OAuthRegistration(org.olat.login.oauth.model.OAuthRegistration) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) MediaResource(org.olat.core.gui.media.MediaResource) RedirectMediaResource(org.olat.core.gui.media.RedirectMediaResource) Identity(org.olat.core.id.Identity) UserRequest(org.olat.core.gui.UserRequest) UserRequestImpl(org.olat.core.gui.UserRequestImpl)

Example 5 with OAuthUser

use of org.olat.login.oauth.model.OAuthUser in project OpenOLAT by OpenOLAT.

the class OAuthDispatcherTest method parseUserInfos_facebook.

@Test
public void parseUserInfos_facebook() throws IOException {
    URL jsonUrl = OAuthDispatcherTest.class.getResource("me_facebook.json");
    String body = IOUtils.toString(jsonUrl, "UTF-8");
    OAuthUser infos = new FacebookProvider().parseInfos(body);
    Assert.assertNotNull(infos);
    Assert.assertEquals("4", infos.getId());
    Assert.assertEquals("John", infos.getFirstName());
    Assert.assertEquals("Smith", infos.getLastName());
    Assert.assertEquals("en_US", infos.getLang());
}
Also used : OAuthUser(org.olat.login.oauth.model.OAuthUser) URL(java.net.URL) FacebookProvider(org.olat.login.oauth.spi.FacebookProvider) Test(org.junit.Test)

Aggregations

OAuthUser (org.olat.login.oauth.model.OAuthUser)36 JSONException (org.json.JSONException)14 JSONObject (org.json.JSONObject)14 Test (org.junit.Test)14 URL (java.net.URL)6 IOException (java.io.IOException)4 FormItem (org.olat.core.gui.components.form.flexible.FormItem)4 UserPropertyHandler (org.olat.user.propertyhandlers.UserPropertyHandler)4 StringReader (java.io.StringReader)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ServletException (javax.servlet.ServletException)2 HttpSession (javax.servlet.http.HttpSession)2 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SecurityGroup (org.olat.basesecurity.SecurityGroup)2 UserRequest (org.olat.core.gui.UserRequest)2 UserRequestImpl (org.olat.core.gui.UserRequestImpl)2 TextElement (org.olat.core.gui.components.form.flexible.elements.TextElement)2 FormLayoutContainer (org.olat.core.gui.components.form.flexible.impl.FormLayoutContainer)2