| author | Pierrick MARIE | 2012-07-03 05:42:42 (EDT) |
|---|---|---|
| committer | Benjamin Cabé | 2012-07-03 05:42:42 (EDT) |
| commit | 7520231369f3d4bb4924bdaeeafa46c0b04adcfd (patch) (side-by-side diff) | |
| tree | ca47261def5d104d5881efd5bd41232324daf9d9 | |
| parent | 8fe1123aa66b266c399fa000889d853c45ed6998 (diff) | |
| download | org.eclipse.koneki.protocols-7520231369f3d4bb4924bdaeeafa46c0b04adcfd.zip org.eclipse.koneki.protocols-7520231369f3d4bb4924bdaeeafa46c0b04adcfd.tar.gz org.eclipse.koneki.protocols-7520231369f3d4bb4924bdaeeafa46c0b04adcfd.tar.bz2 | |
First step to the authentications
4 files changed, 142 insertions, 10 deletions
diff --git a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/META-INF/MANIFEST.MF b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/META-INF/MANIFEST.MF index d0d6d43..4f3fc0b 100644 --- a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/META-INF/MANIFEST.MF +++ b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/META-INF/MANIFEST.MF @@ -10,6 +10,7 @@ Export-Package: org.eclipse.koneki.protocols.omadm.client;version="0.1.0", org.eclipse.koneki.protocols.omadm.client.internal;x-internal:=true Require-Bundle: org.apache.commons.io;bundle-version="1.4.0", org.eclipse.core.runtime;bundle-version="3.4.0" -Import-Package: org.eclipse.koneki.protocols.omadm;version="0.1.0", +Import-Package: org.apache.commons.codec.binary, + org.eclipse.koneki.protocols.omadm;version="0.1.0", org.osgi.framework;version="1.5.0" diff --git a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java index e1b8a21..a6cf11c 100644 --- a/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java +++ b/omadm/bundles/org.eclipse.koneki.protocols.omadm.client/src/org/eclipse/koneki/protocols/omadm/client/basic/DMBasicSession.java @@ -17,6 +17,8 @@ import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URISyntaxException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map; @@ -26,6 +28,7 @@ import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import javax.xml.stream.events.XMLEvent; +import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.input.TeeInputStream; import org.apache.commons.io.output.TeeOutputStream; import org.eclipse.koneki.protocols.omadm.CommandHandler; @@ -75,6 +78,7 @@ final class DMBasicSession implements Runnable { private boolean isManagementPhaseFired; private String currentServerMsgID; private final DMAuthentication authentication; + private String nextNonce; public DMBasicSession(final DMBasicClient dmClient, final URI server, final DMAuthentication userAuth, final URI client, final String sessionId, final DMNode[] devInfoNodes, final CommandHandler commandHandler, final ProtocolListener[] protocolLinsteners, @@ -91,6 +95,7 @@ final class DMBasicSession implements Runnable { this.idGenerator = new DMIDGenerator(); this.statusManager = new DMStatusManager(); this.commandSends = new HashMap<String, Object[]>(); + nextNonce = ""; } @Override @@ -168,8 +173,10 @@ final class DMBasicSession implements Runnable { } private void writeAuthentication(final XMLStreamWriter writer) throws XMLStreamException { - if (!(null == authentication.getAuthentication())) { + // if (!(null == authentication.getAuthentication())) { + switch (authentication.getAuthenticationType()) { + case BASIC: /* * Add basic authentication */ @@ -190,7 +197,59 @@ final class DMBasicSession implements Runnable { writer.writeEndElement(); writer.writeStartElement("Data"); //$NON-NLS-1$ - writer.writeCharacters(new String(authentication.getAuthentication())); + + /* + * Create a Base64 code with the user name and user password values + */ + String userAuth = authentication.getUser() + ":" + authentication.getPassword(); //$NON-NLS-1$ + byte[] B64Auth = Base64.encodeBase64(userAuth.getBytes()); + + writer.writeCharacters(new String(B64Auth)); + writer.writeEndElement(); + + } + writer.writeEndElement(); + /* + * End authentication + */ + break; + /* + * Add md5 authentication + */ + case MD5: + writer.writeStartElement("Cred"); //$NON-NLS-1$ + { + writer.writeStartElement("Meta"); //$NON-NLS-1$ + { + writer.writeStartElement("Format"); //$NON-NLS-1$ + writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$ + writer.writeCharacters("b64"); //$NON-NLS-1$ + writer.writeEndElement(); + + writer.writeStartElement("Type"); //$NON-NLS-1$ + writer.writeAttribute("xmlns", "syncml:metinf"); //$NON-NLS-1$ //$NON-NLS-2$ + writer.writeCharacters("syncml:auth-md5"); //$NON-NLS-1$ + writer.writeEndElement(); + } + writer.writeEndElement(); + + writer.writeStartElement("Data"); //$NON-NLS-1$ + + /* + * Create a Base64 code with the MD5 of the user name and user password values + */ + // + // try { + // MessageDigest m = MessageDigest.getInstance("MD5"); + // m.update(userAuth.getBytes(), 0, userAuth.length()); + // byte[] B64Auth = Base64.encodeBase64(m.digest()); + writer.writeCharacters(computeMd5Authentication()); + + // } catch (NoSuchAlgorithmException e) { + // // TODO Auto-generated catch block + // Activator.logError("There was an error during the md5 authentication", e); //$NON-NLS-1$ + // } + writer.writeEndElement(); } @@ -198,9 +257,37 @@ final class DMBasicSession implements Runnable { /* * End authentication */ + break; } } + private String computeMd5Authentication() { + + String authValue = ""; + + try { + String userAuth = authentication.getUser() + ":" + authentication.getPassword(); //$NON-NLS-1$ + MessageDigest m = MessageDigest.getInstance("MD5"); + + byte[] md5User = m.digest(userAuth.getBytes()); + + byte[] B64User = Base64.encodeBase64(md5User); + + String userNonce = new String(B64User) + ":" + new String(Base64.decodeBase64(nextNonce.getBytes())); + + byte[] md5Nonce = m.digest(userNonce.getBytes()); + + byte[] B64Nonce = Base64.encodeBase64(md5Nonce); + + authValue = new String(B64Nonce); + + } catch (NoSuchAlgorithmException e) { + Activator.logError("There was an error during the md5 authentication", e); //$NON-NLS-1$ + } + + return authValue; + } + private void writeMessage(final OutputStream out) throws XMLStreamException { final XMLStreamWriter writer = this.dmClient.createXMLStreamWriter(out, ENCODING); writer.writeStartDocument(ENCODING, "1.0"); //$NON-NLS-1$ @@ -228,6 +315,7 @@ final class DMBasicSession implements Runnable { writer.writeStartElement("LocURI"); //$NON-NLS-1$ writer.writeCharacters(this.server.toString()); writer.writeEndElement(); + } writer.writeEndElement(); writer.writeStartElement("Source"); //$NON-NLS-1$ @@ -235,6 +323,10 @@ final class DMBasicSession implements Runnable { writer.writeStartElement("LocURI"); //$NON-NLS-1$ writer.writeCharacters(this.client.toString()); writer.writeEndElement(); + + writer.writeStartElement("LocName"); //$NON-NLS-1$ + writer.writeCharacters(authentication.getUser()); + writer.writeEndElement(); } writer.writeEndElement(); diff --git a/omadm/bundles/org.eclipse.koneki.protocols.omadm/src/org/eclipse/koneki/protocols/omadm/AuthenticationType.java b/omadm/bundles/org.eclipse.koneki.protocols.omadm/src/org/eclipse/koneki/protocols/omadm/AuthenticationType.java new file mode 100644 index 0000000..c233733 --- a/dev/null +++ b/omadm/bundles/org.eclipse.koneki.protocols.omadm/src/org/eclipse/koneki/protocols/omadm/AuthenticationType.java @@ -0,0 +1,18 @@ +/******************************************************************************* + * Copyright (c) 2012 Sierra Wireless and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.koneki.protocols.omadm; + +/** + * TODO Comment this class + */ +public enum AuthenticationType { + NONE, BASIC, MD5, HMAC +} diff --git a/omadm/bundles/org.eclipse.koneki.protocols.omadm/src/org/eclipse/koneki/protocols/omadm/DMAuthentication.java b/omadm/bundles/org.eclipse.koneki.protocols.omadm/src/org/eclipse/koneki/protocols/omadm/DMAuthentication.java index 2ba1a0e..f185ade 100644 --- a/omadm/bundles/org.eclipse.koneki.protocols.omadm/src/org/eclipse/koneki/protocols/omadm/DMAuthentication.java +++ b/omadm/bundles/org.eclipse.koneki.protocols.omadm/src/org/eclipse/koneki/protocols/omadm/DMAuthentication.java @@ -15,20 +15,41 @@ package org.eclipse.koneki.protocols.omadm; */ public class DMAuthentication { - private byte[] basicAuthentication; + private AuthenticationType authenticationType; + + private String userName; + private String password; public DMAuthentication() { - basicAuthentication = null; + this.authenticationType = AuthenticationType.NONE; + this.userName = ""; + this.password = ""; + } + + public DMAuthentication(final AuthenticationType authenticationType, final String userName, final String password) { + this.authenticationType = authenticationType; + this.userName = userName; + this.password = password; } - public DMAuthentication(final byte[] basicAuthentication) { - this.basicAuthentication = basicAuthentication; + /** + * @return the authenticationType + */ + public AuthenticationType getAuthenticationType() { + return authenticationType; + } + + /** + * @return the user + */ + public String getUser() { + return userName; } /** - * @return the basicAuthentication + * @return the password */ - public byte[] getAuthentication() { - return basicAuthentication; + public String getPassword() { + return password; } } |

