Search in sources :

Example 6 with Sha256Hash

use of org.apache.shiro.crypto.hash.Sha256Hash in project ANNIS by korpling.

the class AdminServiceImpl method changePassword.

@POST
@Path("users/{userName}/password")
@Consumes("text/plain")
@Produces("application/xml")
public Response changePassword(String newPassword, @PathParam("userName") String userName) {
    Subject requestingUser = SecurityUtils.getSubject();
    requestingUser.checkPermission("admin:write:user");
    ANNISUserConfigurationManager confManager = getConfManager();
    ANNISUserRealm userRealm = getUserRealm();
    if (confManager != null && userRealm != null) {
        User user = confManager.getUser(userName);
        if (user == null) {
            return Response.status(Response.Status.NOT_FOUND).build();
        }
        Shiro1CryptFormat format = new Shiro1CryptFormat();
        SecureRandomNumberGenerator generator = new SecureRandomNumberGenerator();
        // 128 bit
        ByteSource salt = generator.nextBytes(128 / 8);
        Sha256Hash hash = new Sha256Hash(newPassword, salt, 1);
        user.setPasswordHash(format.format(hash));
        if (userRealm.updateUser(user)) {
            return Response.ok().entity(user).build();
        }
    }
    return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("Could not change password").build();
}
Also used : User(annis.security.User) SecureRandomNumberGenerator(org.apache.shiro.crypto.SecureRandomNumberGenerator) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) ANNISUserConfigurationManager(annis.security.ANNISUserConfigurationManager) ByteSource(org.apache.shiro.util.ByteSource) ANNISUserRealm(annis.security.ANNISUserRealm) Subject(org.apache.shiro.subject.Subject) Shiro1CryptFormat(org.apache.shiro.crypto.hash.format.Shiro1CryptFormat) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 7 with Sha256Hash

use of org.apache.shiro.crypto.hash.Sha256Hash in project ANNIS by korpling.

the class ANNISUserRealm method doGetAuthenticationInfo.

@Override
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
    Validate.isInstanceOf(String.class, token.getPrincipal());
    String userName = (String) token.getPrincipal();
    if (userName.equals(anonymousUser)) {
        // for anonymous users the user name equals the Password, so hash the user name
        Sha256Hash hash = new Sha256Hash(userName);
        return new SimpleAuthenticationInfo(userName, hash.getBytes(), ANNISUserRealm.class.getName());
    }
    User user = confManager.getUser(userName);
    if (user != null) {
        String passwordHash = user.getPasswordHash();
        if (passwordHash != null) {
            if (passwordHash.startsWith("$")) {
                Shiro1CryptFormat fmt = new Shiro1CryptFormat();
                Hash hashCredentials = fmt.parse(passwordHash);
                if (hashCredentials instanceof SimpleHash) {
                    SimpleHash simpleHash = (SimpleHash) hashCredentials;
                    Validate.isTrue(simpleHash.getIterations() == 1, "Hash iteration count must be 1 for every password hash!");
                    // actually set the information from the user file
                    SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(userName, simpleHash.getBytes(), ANNISUserRealm.class.getName());
                    info.setCredentialsSalt(new SerializableByteSource(simpleHash.getSalt()));
                    return info;
                }
            } else {
                // fallback unsalted hex hash
                SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(token.getPrincipal(), passwordHash, ANNISUserRealm.class.getName());
                return info;
            }
        }
    }
    return null;
}
Also used : SimpleAuthenticationInfo(org.apache.shiro.authc.SimpleAuthenticationInfo) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) Hash(org.apache.shiro.crypto.hash.Hash) SimpleHash(org.apache.shiro.crypto.hash.SimpleHash) Shiro1CryptFormat(org.apache.shiro.crypto.hash.format.Shiro1CryptFormat)

Example 8 with Sha256Hash

use of org.apache.shiro.crypto.hash.Sha256Hash in project shiro by apache.

the class JDBCRealmTest method createDefaultSchema.

/**
 * Creates a test database with the default (no separate salt column) schema, salting with
 * username if salted is true. Sets the DataSource of the realm associated with the test
 * to a DataSource connected to the database.  (To prevent concurrency problems when tests
 * are executed in multithreaded mode, each test method gets its own database.)
 */
protected void createDefaultSchema(String testName, boolean salted) {
    jdbcDataSource ds = new jdbcDataSource();
    ds.setDatabase("jdbc:hsqldb:mem:" + name);
    ds.setUser("SA");
    ds.setPassword("");
    Connection conn = null;
    Statement sql = null;
    try {
        conn = ds.getConnection();
        sql = conn.createStatement();
        sql.executeUpdate("create table users (username varchar(20), password varchar(20))");
        Sha256Hash sha256Hash = salted ? new Sha256Hash(plainTextPassword, salt) : new Sha256Hash(plainTextPassword);
        String password = sha256Hash.toHex();
        sql.executeUpdate("insert into users values ('" + username + "', '" + password + "')");
    } catch (SQLException ex) {
        Assert.fail("Exception creating test database");
    } finally {
        JdbcUtils.closeStatement(sql);
        JdbcUtils.closeConnection(conn);
    }
    createRolesAndPermissions(ds);
    realmMap.get(testName).setDataSource(ds);
    dsMap.put(testName, ds);
}
Also used : org.hsqldb.jdbc.jdbcDataSource(org.hsqldb.jdbc.jdbcDataSource) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) Connection(java.sql.Connection)

Example 9 with Sha256Hash

use of org.apache.shiro.crypto.hash.Sha256Hash in project nutzboot by nutzam.

the class UserModule method login.

@Ok("json")
@Fail("http:500")
@POST
@At("/login")
public boolean login(@Param("username") String username, @Param("password") String password, HttpSession session) {
    User user = dao.fetch(User.class, username);
    if (user == null)
        return false;
    Sha256Hash hash = new Sha256Hash(password, user.getSalt());
    if (!hash.toHex().equals(user.getPassword())) {
        return false;
    }
    Subject subject = SecurityUtils.getSubject();
    subject.login(new SimpleShiroToken(user.getId()));
    return true;
}
Also used : User(io.nutz.demo.simple.bean.User) Sha256Hash(org.apache.shiro.crypto.hash.Sha256Hash) SimpleShiroToken(org.nutz.integration.shiro.SimpleShiroToken) Subject(org.apache.shiro.subject.Subject) At(org.nutz.mvc.annotation.At) POST(org.nutz.mvc.annotation.POST) Ok(org.nutz.mvc.annotation.Ok) Fail(org.nutz.mvc.annotation.Fail)

Aggregations

Sha256Hash (org.apache.shiro.crypto.hash.Sha256Hash)9 User (io.nutz.demo.simple.bean.User)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Shiro1CryptFormat (org.apache.shiro.crypto.hash.format.Shiro1CryptFormat)2 Subject (org.apache.shiro.subject.Subject)2 org.hsqldb.jdbc.jdbcDataSource (org.hsqldb.jdbc.jdbcDataSource)2 JdbcTemplate (org.springframework.jdbc.core.JdbcTemplate)2 ANNISUserConfigurationManager (annis.security.ANNISUserConfigurationManager)1 ANNISUserRealm (annis.security.ANNISUserRealm)1 User (annis.security.User)1 Date (java.util.Date)1 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 SimpleAuthenticationInfo (org.apache.shiro.authc.SimpleAuthenticationInfo)1 SecureRandomNumberGenerator (org.apache.shiro.crypto.SecureRandomNumberGenerator)1 Hash (org.apache.shiro.crypto.hash.Hash)1