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;
}
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();
}
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();
}
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();
}
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();
}
Aggregations