Skip to content

Instantly share code, notes, and snippets.

@mdeggies
Created April 13, 2017 20:36
Show Gist options
  • Select an option

  • Save mdeggies/83e7a37be96595ec6b4c71cd481dbd5a to your computer and use it in GitHub Desktop.

Select an option

Save mdeggies/83e7a37be96595ec6b4c71cd481dbd5a to your computer and use it in GitHub Desktop.

Revisions

  1. Michele Degges created this gist Apr 13, 2017.
    75 changes: 75 additions & 0 deletions pwd-import-java.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    """
    password-import.java
    ~~~~~~~~~
    A super basic script that shows how to import a user with a freshly SHA-1 hashed password into Stormpath
    and authenticate him using the Java SDK (version 1.5.5): https://github.com/stormpath/stormpath-sdk-java
    NOTE: You will need to add the apache commons codec jar (version 1.10) to your project in order to use DigestUtils
    """

    public class Quickstart {

    private static final Logger log = LoggerFactory.getLogger(quickstart.class);
    private static String APPLICATION_NAME = "My Application";

    public static void main(String[] args) {

    ClientBuilder builder = Clients.builder();
    Client client = builder.build();

    Tenant tenant = client.getCurrentTenant();
    log.info("Current Tenant: " + tenant.getHref() + ", " + tenant.getName());

    ApplicationList applications = tenant.getApplications(
    Applications.where(Applications.name().eqIgnoreCase(APPLICATION_NAME))
    );

    Application application = applications.iterator().next();
    log.info("Application: " + application.getHref() + ", " + application.getName());

    Account account = client.instantiate(Account.class);

    //Create the sha-1 hash of the password & return it as a byte array
    byte[] byte_array = DigestUtils.sha1("Changeme1");
    //Base 64 encode the byte array and convert it into a string
    String pwd = Base64.getEncoder().encodeToString(byte_array);

    //Create the password string according to the docs: https://docs.stormpath.com/java/product-guide/latest/accnt_mgmt.html#importing-accounts-with-supported-password-hashes
    String MCFEncode = "$stormpath2$SHA-1$1$$" + pwd;

    //Set the account properties
    account.setGivenName("Joe")
    .setSurname("Quickstart_Stormtrooper")
    .setUsername("tk421")
    .setEmail("[email protected]")
    .setPassword(MCFEncode);

    // Create the account! :)
    account = application.createAccount(Accounts.newCreateRequestFor(account).setPasswordFormat(PasswordFormat.MCF).build());

    // Search for the newly created User Account
    Map<String, Object> queryParams = new HashMap<String, Object>();
    queryParams.put("email", "[email protected]");
    AccountList accounts = application.getAccounts(queryParams);
    account = accounts.iterator().next();

    log.info("Found Account: " + account.getHref() + ", " + account.getEmail());

    String usernameOrEmail = "[email protected]";
    String rawPassword = "Changeme1";

    // Create an authentication request using the credentials
    AuthenticationRequest request = UsernamePasswordRequests.builder()
    .setUsernameOrEmail(usernameOrEmail)
    .setPassword(rawPassword)
    .build();

    // Authenticate the account against the application:
    try {
    AuthenticationResult result = application.authenticateAccount(request);
    account = result.getAccount();
    log.info("Authenticated Account: " + account.getUsername() + ", Email: " + account.getEmail());
    } catch (ResourceException ex) {
    log.error(ex.getMessage());
    }
    }