Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save anton-x-t/43a298607937c46bbdf9e102ddeec332 to your computer and use it in GitHub Desktop.

Select an option

Save anton-x-t/43a298607937c46bbdf9e102ddeec332 to your computer and use it in GitHub Desktop.

Revisions

  1. Anton Thelander revised this gist Oct 2, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Thank you very much eogiles for earlier written code, credit goes to you.
    // Thank you very much javajack for earlier written code, credit goes to you.
    // License for this code: cc by-sa 3.0

    // Please get Maven in order for this code to work.
  2. Anton Thelander revised this gist Oct 1, 2018. 1 changed file with 59 additions and 109 deletions.
    168 changes: 59 additions & 109 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,111 +1,61 @@
    // License for this code: cc by-sa 3.0

    package sample;

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;

    import java.security.MessageDigest;
    import java.security.SecureRandom;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Set;

    import java.util.TimeZone;

    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.ws.handler.MessageContext;
    import javax.xml.ws.handler.soap.SOAPHandler;
    import javax.xml.ws.handler.soap.SOAPMessageContext;

    import sun.misc.BASE64Encoder;

    public class SimpleWsAuthHandler implements SOAPHandler<SOAPMessageContext> {

    public boolean handleMessage(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {

    try {
    String usernameStr = "myusername";
    String passwordStr = "myPassword";

    //From the spec: Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) )
    //Make the nonce
    SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
    rand.setSeed(System.currentTimeMillis());
    byte[] nonceBytes = new byte[16];
    rand.nextBytes(nonceBytes);

    //Make the created date
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    String createdDate = df.format(Calendar.getInstance().getTime());
    byte[] createdDateBytes = createdDate.getBytes("UTF-8");

    //Make the password
    byte[] passwordBytes = passwordStr.getBytes("UTF-8");

    //SHA-1 hash the bunch of it.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(nonceBytes);
    baos.write(createdDateBytes);
    baos.write(passwordBytes);
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] digestedPassword = md.digest(baos.toByteArray());

    //Encode the password and nonce for sending
    String passwordB64 = (new BASE64Encoder()).encode(digestedPassword);
    String nonceB64 = (new BASE64Encoder()).encode(nonceBytes);

    //Now create the header with all the appropriate elements
    SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
    SOAPHeader header = envelope.addHeader();
    SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");

    SOAPElement username = usernameToken.addChildElement("Username", "wsse");
    username.addTextNode(usernameStr);

    SOAPElement password = usernameToken.addChildElement("Password", "wsse");
    password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
    password.addTextNode(passwordB64);

    SOAPElement nonce = usernameToken.addChildElement("Nonce", "wsse");
    nonce.addTextNode(nonceB64);

    SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    created.addTextNode(createdDate);

    smc.getMessage().writeTo(System.out);

    } catch (Exception e) {
    throw new RuntimeException(e);
    }

    }

    return outboundProperty;

    }

    public Set getHeaders() {
    return Collections.emptySet();
    }

    public void close(MessageContext context) {
    }
    // License for this code: cc by-sa 3.0

    // Please get Maven in order for this code to work.

    public boolean handleFault(SOAPMessageContext context) {
    return false;
    }
    }
    // Please get your own imports, you'll probably have to get commons-codec manually by adding it to your POM in Maven:
    // <!-- https://mvnrepository.com/artifact/commons-codec/commons-codec
    // To be able to convert to Base64 when making the PasswordDigest token. -->
    // <dependency>
    // <groupId>commons-codec</groupId>
    // <artifactId>commons-codec</artifactId>
    // <version>1.11</version>
    // </dependency>

    // On building a complete security header including nonce, createdTime and passwordDigest,
    // thank you eogiles and Rakesh Waghela (javajack), https://gist.github.com/propatience/43a298607937c46bbdf9e102ddeec332
    private static String[] buildSecurityHeader(String passwordParam) throws IOException, NoSuchAlgorithmException {
    String password = passwordParam;

    // From the spec: Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) )
    // Make the nonce
    SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
    rand.setSeed(System.currentTimeMillis());
    byte[] nonceBytes = new byte[16];
    rand.nextBytes(nonceBytes);

    // Make the created date
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    String createdDate = df.format(Calendar.getInstance().getTime());
    byte[] createdDateBytes = createdDate.getBytes("UTF-8");

    // Make the password
    byte[] passwordBytes = password.getBytes("UTF-8");

    // SHA-1 hash the bunch of it.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(nonceBytes);
    baos.write(createdDateBytes);
    baos.write(passwordBytes);
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] digestedPassword = md.digest(baos.toByteArray());

    // Encode the password and nonce for sending
    // String passwordB64 = (new BASE64Encoder()).encode(digestedPassword);
    // On "Base64Encoder cannot be resolved", thank you Sai, https://stackoverflow.com/q/6526883
    byte[] passwordDigestB64 = org.apache.commons.codec.binary.Base64.encodeBase64(digestedPassword);

    String passwordDigest = new String(passwordDigestB64);

    byte[] nonceB64 = org.apache.commons.codec.binary.Base64.encodeBase64(nonceBytes);

    String nonce = new String(nonceB64);

    // On initializing String[], thank you glmxndr, https://stackoverflow.com/q/1200621
    String returnString[] = new String[3];
    returnString[0] = nonce;
    returnString[1] = createdDate;
    returnString[2] = passwordDigest;

    return returnString;
    }
  3. Anton Thelander revised this gist Oct 1, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // License for this code: cc by-sa 3.0

    package sample;

    import java.io.ByteArrayOutputStream;
  4. @javajack javajack revised this gist Dec 3, 2015. No changes.
  5. @eogiles eogiles revised this gist Jun 5, 2013. 1 changed file with 81 additions and 81 deletions.
    162 changes: 81 additions & 81 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -25,85 +25,85 @@

    import sun.misc.BASE64Encoder;

    public class SimpleWsAuthHandler implements SOAPHandler&lt;SOAPMessageContext&gt; {

    public boolean handleMessage(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {

    try {
    String usernameStr = "myusername";
    String passwordStr = "myPassword";

    //From the spec: Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) )
    //Make the nonce
    SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
    rand.setSeed(System.currentTimeMillis());
    byte[] nonceBytes = new byte[16];
    rand.nextBytes(nonceBytes);

    //Make the created date
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    String createdDate = df.format(Calendar.getInstance().getTime());
    byte[] createdDateBytes = createdDate.getBytes("UTF-8");

    //Make the password
    byte[] passwordBytes = passwordStr.getBytes("UTF-8");

    //SHA-1 hash the bunch of it.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(nonceBytes);
    baos.write(createdDateBytes);
    baos.write(passwordBytes);
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] digestedPassword = md.digest(baos.toByteArray());

    //Encode the password and nonce for sending
    String passwordB64 = (new BASE64Encoder()).encode(digestedPassword);
    String nonceB64 = (new BASE64Encoder()).encode(nonceBytes);

    //Now create the header with all the appropriate elements
    SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
    SOAPHeader header = envelope.addHeader();
    SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");

    SOAPElement username = usernameToken.addChildElement("Username", "wsse");
    username.addTextNode(usernameStr);

    SOAPElement password = usernameToken.addChildElement("Password", "wsse");
    password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
    password.addTextNode(passwordB64);

    SOAPElement nonce = usernameToken.addChildElement("Nonce", "wsse");
    nonce.addTextNode(nonceB64);

    SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    created.addTextNode(createdDate);

    smc.getMessage().writeTo(System.out);

    } catch (Exception e) {
    throw new RuntimeException(e);
    }

    }

    return outboundProperty;

    }

    public Set getHeaders() {
    return Collections.emptySet();
    }

    public void close(MessageContext context) {
    }


    public boolean handleFault(SOAPMessageContext context) {
    return false;
    }
    public class SimpleWsAuthHandler implements SOAPHandler<SOAPMessageContext> {

    public boolean handleMessage(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);
    if (outboundProperty.booleanValue()) {
    try {
    String usernameStr = "myusername";
    String passwordStr = "myPassword";
    //From the spec: Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) )
    //Make the nonce
    SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
    rand.setSeed(System.currentTimeMillis());
    byte[] nonceBytes = new byte[16];
    rand.nextBytes(nonceBytes);
    //Make the created date
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    String createdDate = df.format(Calendar.getInstance().getTime());
    byte[] createdDateBytes = createdDate.getBytes("UTF-8");
    //Make the password
    byte[] passwordBytes = passwordStr.getBytes("UTF-8");
    //SHA-1 hash the bunch of it.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(nonceBytes);
    baos.write(createdDateBytes);
    baos.write(passwordBytes);
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] digestedPassword = md.digest(baos.toByteArray());
    //Encode the password and nonce for sending
    String passwordB64 = (new BASE64Encoder()).encode(digestedPassword);
    String nonceB64 = (new BASE64Encoder()).encode(nonceBytes);
    //Now create the header with all the appropriate elements
    SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
    SOAPHeader header = envelope.addHeader();
    SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
    SOAPElement username = usernameToken.addChildElement("Username", "wsse");
    username.addTextNode(usernameStr);
    SOAPElement password = usernameToken.addChildElement("Password", "wsse");
    password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
    password.addTextNode(passwordB64);
    SOAPElement nonce = usernameToken.addChildElement("Nonce", "wsse");
    nonce.addTextNode(nonceB64);
    SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    created.addTextNode(createdDate);
    smc.getMessage().writeTo(System.out);
    } catch (Exception e) {
    throw new RuntimeException(e);
    }
    }
    return outboundProperty;
    }
    public Set getHeaders() {
    return Collections.emptySet();
    }
    public void close(MessageContext context) {
    }
    public boolean handleFault(SOAPMessageContext context) {
    return false;
    }
    }
  6. @eogiles eogiles created this gist Jun 5, 2013.
    109 changes: 109 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,109 @@
    package sample;

    import java.io.ByteArrayOutputStream;
    import java.io.IOException;

    import java.security.MessageDigest;
    import java.security.SecureRandom;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;

    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Set;

    import java.util.TimeZone;

    import javax.xml.soap.SOAPElement;
    import javax.xml.soap.SOAPEnvelope;
    import javax.xml.soap.SOAPException;
    import javax.xml.soap.SOAPHeader;
    import javax.xml.ws.handler.MessageContext;
    import javax.xml.ws.handler.soap.SOAPHandler;
    import javax.xml.ws.handler.soap.SOAPMessageContext;

    import sun.misc.BASE64Encoder;

    public class SimpleWsAuthHandler implements SOAPHandler&lt;SOAPMessageContext&gt; {

    public boolean handleMessage(SOAPMessageContext smc) {
    Boolean outboundProperty = (Boolean) smc.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

    if (outboundProperty.booleanValue()) {

    try {
    String usernameStr = "myusername";
    String passwordStr = "myPassword";

    //From the spec: Password_Digest = Base64 ( SHA-1 ( nonce + created + password ) )
    //Make the nonce
    SecureRandom rand = SecureRandom.getInstance("SHA1PRNG");
    rand.setSeed(System.currentTimeMillis());
    byte[] nonceBytes = new byte[16];
    rand.nextBytes(nonceBytes);

    //Make the created date
    DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    df.setTimeZone(TimeZone.getTimeZone("UTC"));
    String createdDate = df.format(Calendar.getInstance().getTime());
    byte[] createdDateBytes = createdDate.getBytes("UTF-8");

    //Make the password
    byte[] passwordBytes = passwordStr.getBytes("UTF-8");

    //SHA-1 hash the bunch of it.
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    baos.write(nonceBytes);
    baos.write(createdDateBytes);
    baos.write(passwordBytes);
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    byte[] digestedPassword = md.digest(baos.toByteArray());

    //Encode the password and nonce for sending
    String passwordB64 = (new BASE64Encoder()).encode(digestedPassword);
    String nonceB64 = (new BASE64Encoder()).encode(nonceBytes);

    //Now create the header with all the appropriate elements
    SOAPEnvelope envelope = smc.getMessage().getSOAPPart().getEnvelope();
    SOAPHeader header = envelope.addHeader();
    SOAPElement security = header.addChildElement("Security", "wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
    SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");

    SOAPElement username = usernameToken.addChildElement("Username", "wsse");
    username.addTextNode(usernameStr);

    SOAPElement password = usernameToken.addChildElement("Password", "wsse");
    password.setAttribute("Type", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest");
    password.addTextNode(passwordB64);

    SOAPElement nonce = usernameToken.addChildElement("Nonce", "wsse");
    nonce.addTextNode(nonceB64);

    SOAPElement created = usernameToken.addChildElement("Created", "wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
    created.addTextNode(createdDate);

    smc.getMessage().writeTo(System.out);

    } catch (Exception e) {
    throw new RuntimeException(e);
    }

    }

    return outboundProperty;

    }

    public Set getHeaders() {
    return Collections.emptySet();
    }

    public void close(MessageContext context) {
    }


    public boolean handleFault(SOAPMessageContext context) {
    return false;
    }
    }