Search in sources :

Example 31 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project SSM by Intel-bigdata.

the class LoginRestApi method loginWithZeppelinCredential.

private JsonResponse loginWithZeppelinCredential(Subject currentUser) {
    JsonResponse response = null;
    // Use the default username/password to generate a token to login.
    // This username/password is consistent with the one in conf/shiro.ini.
    String userName = "admin";
    String password = "ssm123";
    try {
        UsernamePasswordToken token = new UsernamePasswordToken(userName, password);
        // token.setRememberMe(true);
        currentUser.getSession().stop();
        currentUser.getSession(true);
        // Login will fail if username/password doesn't match with the one
        // configured in conf/shiro.ini.
        currentUser.login(token);
        HashSet<String> roles = SecurityUtils.getRoles();
        String principal = SecurityUtils.getPrincipal();
        String ticket;
        if ("anonymous".equals(principal))
            ticket = "anonymous";
        else
            ticket = TicketContainer.instance.getTicket(principal);
        Map<String, String> data = new HashMap<>();
        data.put("principal", principal);
        data.put("roles", roles.toString());
        data.put("ticket", ticket);
        response = new JsonResponse(Response.Status.OK, "", data);
        // if no exception, that's it, we're done!
        // set roles for user in NotebookAuthorization module
        NotebookAuthorization.getInstance().setRoles(principal, roles);
    } catch (UnknownAccountException uae) {
        // username wasn't in the system, show them an error message?
        LOG.error("Exception in login: ", uae);
    } catch (IncorrectCredentialsException ice) {
        // password didn't match, try again?
        LOG.error("Exception in login: ", ice);
    } catch (LockedAccountException lae) {
        // account for that username is locked - can't login.  Show them a message?
        LOG.error("Exception in login: ", lae);
    } catch (AuthenticationException ae) {
        // unexpected condition - error?
        LOG.error("Exception in login: ", ae);
    }
    return response;
}
Also used : HashMap(java.util.HashMap) JsonResponse(org.apache.zeppelin.server.JsonResponse)

Example 32 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project SSM by Intel-bigdata.

the class LoginRestApi method postPassword.

@POST
@Path("newPassword")
@ZeppelinApi
public Response postPassword(@FormParam("userName") String userName, @FormParam("oldPassword") String oldPassword, @FormParam("newPassword1") String newPassword, @FormParam("newPassword2") String newPassword2) {
    LOG.info("Trying to change password for user: " + userName);
    JsonResponse response = null;
    // ticket set to anonymous for anonymous user. Simplify testing.
    Subject currentUser = org.apache.shiro.SecurityUtils.getSubject();
    if (currentUser.isAuthenticated()) {
        currentUser.logout();
    }
    boolean isCorrectCredential = false;
    try {
        String password = StringUtil.toSHA512String(oldPassword);
        isCorrectCredential = engine.getCmdletManager().authentic(new UserInfo(userName, password));
    } catch (Exception e) {
        LOG.error("Exception in login: ", e);
    }
    if (isCorrectCredential) {
        if (newPassword.equals(newPassword2)) {
            try {
                engine.getCmdletManager().newPassword(new UserInfo(userName, newPassword));
                LOG.info("The password has been changed for user: " + userName);
            } catch (Exception e) {
                LOG.error("Exception in setting password: ", e);
            }
        } else {
            LOG.warn("Unmatched password typed in two times, please do it again!");
        }
    }
    // Re-login
    if (!currentUser.isAuthenticated() && isCorrectCredential) {
        response = loginWithZeppelinCredential(currentUser);
    }
    if (response == null) {
        LOG.warn("Incorrect credential for changing password!");
        response = new JsonResponse(Response.Status.FORBIDDEN, "", "");
    }
    return response.build();
}
Also used : UserInfo(org.smartdata.model.UserInfo) JsonResponse(org.apache.zeppelin.server.JsonResponse) Subject(org.apache.shiro.subject.Subject) MetaStoreException(org.smartdata.metastore.MetaStoreException) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 33 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project SSM by Intel-bigdata.

the class LoginRestApi method postAddUser.

/**
 * Adds new user. Only admin user has the permission.
 *
 * @param userName the new user's name to be added
 * @param password1 the new user's password
 * @param password2 the new user's password for verification.
 * @return
 */
@POST
@Path("adduser")
@ZeppelinApi
public Response postAddUser(@FormParam("adminPassword") String adminPassword, @FormParam("userName") String userName, @FormParam("password1") String password1, @FormParam("password2") String password2) {
    Subject currentUser = org.apache.shiro.SecurityUtils.getSubject();
    if (!password1.equals(password2)) {
        String msg = "Unmatched password typed in two times!";
        LOG.warn(msg);
        return new JsonResponse(Response.Status.BAD_REQUEST, msg, "").build();
    }
    String password = StringUtil.toSHA512String(adminPassword);
    try {
        boolean hasCredential = engine.getCmdletManager().authentic(new UserInfo(SSM_ADMIN, password));
        if (hasCredential && currentUser.isAuthenticated()) {
            engine.getCmdletManager().addNewUser(new UserInfo(userName, password1));
        } else {
            String msg = "The typed admin password is not correct!";
            LOG.warn(msg + " Failed to register new user!");
            return new JsonResponse(Response.Status.FORBIDDEN, msg, "").build();
        }
    } catch (MetaStoreException e) {
        LOG.warn(e.getMessage());
        return new JsonResponse(Response.Status.BAD_REQUEST, e.getMessage(), "").build();
    }
    return new JsonResponse(Response.Status.OK, "", "").build();
}
Also used : MetaStoreException(org.smartdata.metastore.MetaStoreException) UserInfo(org.smartdata.model.UserInfo) Subject(org.apache.shiro.subject.Subject) JsonResponse(org.apache.zeppelin.server.JsonResponse) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) POST(javax.ws.rs.POST)

Example 34 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project SSM by Intel-bigdata.

the class SecurityRestApi method ticket.

/**
 * Get ticket
 * Returns username & ticket
 * for anonymous access, username is always anonymous.
 * After getting this ticket, access through websockets become safe
 *
 * @return 200 response
 */
@GET
@Path("ticket")
@ZeppelinApi
public Response ticket() {
    ZeppelinConfiguration conf = ZeppelinConfiguration.create();
    String principal = SecurityUtils.getPrincipal();
    HashSet<String> roles = SecurityUtils.getRoles();
    JsonResponse response;
    // ticket set to anonymous for anonymous user. Simplify testing.
    String ticket;
    if ("anonymous".equals(principal))
        ticket = "anonymous";
    else
        ticket = TicketContainer.instance.getTicket(principal);
    Map<String, String> data = new HashMap<>();
    data.put("principal", principal);
    data.put("roles", roles.toString());
    data.put("ticket", ticket);
    response = new JsonResponse(Response.Status.OK, "", data);
    LOG.debug(response.toString());
    return response.build();
}
Also used : ZeppelinConfiguration(org.apache.zeppelin.conf.ZeppelinConfiguration) JsonResponse(org.apache.zeppelin.server.JsonResponse) Path(javax.ws.rs.Path) ZeppelinApi(org.apache.zeppelin.annotation.ZeppelinApi) GET(javax.ws.rs.GET)

Example 35 with JsonResponse

use of org.apache.zeppelin.server.JsonResponse in project SSM by Intel-bigdata.

the class CredentialRestApi method removeCredentials.

/**
 * Remove User Credentials REST API
 * @param
 * @return JSON with status.OK
 * @throws IOException, IllegalArgumentException
 */
@DELETE
public Response removeCredentials(String message) throws IOException, IllegalArgumentException {
    String user = SecurityUtils.getPrincipal();
    logger.info("removeCredentials credentials for user {} ", user);
    UserCredentials uc = credentials.removeUserCredentials(user);
    if (uc == null) {
        return new JsonResponse(Status.NOT_FOUND).build();
    }
    return new JsonResponse(Status.OK).build();
}
Also used : UserCredentials(org.apache.zeppelin.user.UserCredentials) JsonResponse(org.apache.zeppelin.server.JsonResponse)

Aggregations

JsonResponse (org.apache.zeppelin.server.JsonResponse)37 ZeppelinApi (org.apache.zeppelin.annotation.ZeppelinApi)22 Path (javax.ws.rs.Path)20 IOException (java.io.IOException)12 POST (javax.ws.rs.POST)12 UserCredentials (org.apache.zeppelin.user.UserCredentials)8 PUT (javax.ws.rs.PUT)6 Note (org.apache.zeppelin.notebook.Note)6 GET (javax.ws.rs.GET)5 Paragraph (org.apache.zeppelin.notebook.Paragraph)5 AuthenticationInfo (org.apache.zeppelin.user.AuthenticationInfo)5 HashMap (java.util.HashMap)4 Subject (org.apache.shiro.subject.Subject)4 ZeppelinConfiguration (org.apache.zeppelin.conf.ZeppelinConfiguration)4 InterpreterException (org.apache.zeppelin.interpreter.InterpreterException)4 Map (java.util.Map)3 DELETE (javax.ws.rs.DELETE)3 InterpreterSetting (org.apache.zeppelin.interpreter.InterpreterSetting)3 MetaStoreException (org.smartdata.metastore.MetaStoreException)3 UserInfo (org.smartdata.model.UserInfo)3