Search in sources :

Example 1 with OBinaryToken

use of com.orientechnologies.orient.server.binary.impl.OBinaryToken in project orientdb by orientechnologies.

the class OBinaryTokenSerializer method deserialize.

public OBinaryToken deserialize(InputStream stream) throws IOException {
    DataInputStream input = new DataInputStream(stream);
    OrientJwtHeader header = new OrientJwtHeader();
    header.setType(types[input.readByte()]);
    header.setKeyId(keys[input.readByte()]);
    header.setAlgorithm(algorithms[input.readByte()]);
    OBinaryToken token = new OBinaryToken();
    token.setHeader(header);
    token.setDatabase(readString(input));
    byte pos = input.readByte();
    if (pos >= 0)
        token.setDatabaseType(dbTypes[pos]);
    short cluster = input.readShort();
    long position = input.readLong();
    if (cluster != -1 && position != -1)
        token.setUserRid(new ORecordId(cluster, position));
    token.setExpiry(input.readLong());
    token.setServerUser(input.readBoolean());
    if (token.isServerUser()) {
        token.setUserName(readString(input));
    }
    token.setProtocolVersion(input.readShort());
    token.setSerializer(readString(input));
    token.setDriverName(readString(input));
    token.setDriverVersion(readString(input));
    return token;
}
Also used : DataInputStream(java.io.DataInputStream) OBinaryToken(com.orientechnologies.orient.server.binary.impl.OBinaryToken) ORecordId(com.orientechnologies.orient.core.id.ORecordId)

Example 2 with OBinaryToken

use of com.orientechnologies.orient.server.binary.impl.OBinaryToken in project orientdb by orientechnologies.

the class OTokenHandlerImpl method getSignedBinaryToken.

public byte[] getSignedBinaryToken(final ODatabaseDocumentInternal db, final OSecurityUser user, final ONetworkProtocolData data) {
    try {
        final OBinaryToken token = new OBinaryToken();
        long curTime = System.currentTimeMillis();
        final OrientJwtHeader header = new OrientJwtHeader();
        header.setAlgorithm(algorithm);
        header.setKeyId(keyProvider.getDefaultKey());
        header.setType("OrientDB");
        token.setHeader(header);
        if (db != null) {
            token.setDatabase(db.getName());
            token.setDatabaseType(db.getStorage().getUnderlying().getType());
        }
        if (data.serverUser) {
            token.setServerUser(true);
            token.setUserName(data.serverUsername);
        }
        if (user != null)
            token.setUserRid(user.getIdentity().getIdentity());
        token.setExpiry(curTime + sessionInMills);
        token.setProtocolVersion(data.protocolVersion);
        token.setSerializer(data.serializationImpl);
        token.setDriverName(data.driverName);
        token.setDriverVersion(data.driverVersion);
        return serializeSignedToken(token);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw OException.wrapException(new OSystemException("Error on token parsing"), e);
    }
}
Also used : OSystemException(com.orientechnologies.common.exception.OSystemException) OBinaryToken(com.orientechnologies.orient.server.binary.impl.OBinaryToken) OException(com.orientechnologies.common.exception.OException) OSystemException(com.orientechnologies.common.exception.OSystemException) OTokenException(com.orientechnologies.orient.core.metadata.security.OTokenException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 3 with OBinaryToken

use of com.orientechnologies.orient.server.binary.impl.OBinaryToken in project orientdb by orientechnologies.

the class OTokenHandlerImpl method renewIfNeeded.

@Override
public byte[] renewIfNeeded(final OToken token) {
    if (token == null)
        throw new IllegalArgumentException("Token is null");
    final long curTime = System.currentTimeMillis();
    if (token.getExpiry() - curTime < (sessionInMills / 2) && token.getExpiry() >= curTime) {
        final long expiryMinutes = sessionInMills;
        final long currTime = System.currentTimeMillis();
        token.setExpiry(currTime + expiryMinutes);
        try {
            if (token instanceof OBinaryToken)
                return serializeSignedToken((OBinaryToken) token);
            else
                throw new OTokenException("renew of web token not supported");
        } catch (IOException e) {
            throw OException.wrapException(new OSystemException("Error on token parsing"), e);
        }
    }
    return OCommonConst.EMPTY_BYTE_ARRAY;
}
Also used : OTokenException(com.orientechnologies.orient.core.metadata.security.OTokenException) OSystemException(com.orientechnologies.common.exception.OSystemException) OBinaryToken(com.orientechnologies.orient.server.binary.impl.OBinaryToken)

Example 4 with OBinaryToken

use of com.orientechnologies.orient.server.binary.impl.OBinaryToken in project orientdb by orientechnologies.

the class OBinaryTokenSerializerTest method testSerializerDeserializeServerUserToken.

@Test
public void testSerializerDeserializeServerUserToken() throws IOException {
    OBinaryToken token = new OBinaryToken();
    token.setDatabase("test");
    token.setDatabaseType("plocal");
    token.setUserRid(new ORecordId(43, 234));
    OrientJwtHeader header = new OrientJwtHeader();
    header.setKeyId("key");
    header.setAlgorithm("HmacSHA256");
    header.setType("OrientDB");
    token.setHeader(header);
    token.setExpiry(20L);
    token.setServerUser(true);
    token.setUserName("aaa");
    token.setProtocolVersion((short) 2);
    token.setSerializer("ser");
    token.setDriverName("aa");
    token.setDriverVersion("aa");
    ByteArrayOutputStream bas = new ByteArrayOutputStream();
    ser.serialize(token, bas);
    ByteArrayInputStream input = new ByteArrayInputStream(bas.toByteArray());
    OBinaryToken tok = ser.deserialize(input);
    assertEquals("test", token.getDatabase());
    assertEquals("plocal", token.getDatabaseType());
    ORID id = token.getUserId();
    assertEquals(43, id.getClusterId());
    assertEquals(20L, tok.getExpiry());
    assertTrue(token.isServerUser());
    assertEquals("aaa", tok.getUserName());
    assertEquals("OrientDB", tok.getHeader().getType());
    assertEquals("HmacSHA256", tok.getHeader().getAlgorithm());
    assertEquals("key", tok.getHeader().getKeyId());
    assertEquals((short) 2, tok.getProtocolVersion());
    assertEquals("ser", tok.getSerializer());
    assertEquals("aa", tok.getDriverName());
    assertEquals("aa", tok.getDriverVersion());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ORID(com.orientechnologies.orient.core.id.ORID) OBinaryToken(com.orientechnologies.orient.server.binary.impl.OBinaryToken) ORecordId(com.orientechnologies.orient.core.id.ORecordId) Test(org.junit.Test)

Example 5 with OBinaryToken

use of com.orientechnologies.orient.server.binary.impl.OBinaryToken in project orientdb by orientechnologies.

the class OBinaryTokenSerializerTest method testSerializerDeserializeNullInfoUserToken.

@Test
public void testSerializerDeserializeNullInfoUserToken() throws IOException {
    OBinaryToken token = new OBinaryToken();
    token.setDatabase(null);
    token.setDatabaseType(null);
    token.setUserRid(null);
    OrientJwtHeader header = new OrientJwtHeader();
    header.setKeyId("key");
    header.setAlgorithm("HmacSHA256");
    header.setType("OrientDB");
    token.setHeader(header);
    token.setExpiry(20L);
    token.setServerUser(true);
    token.setUserName("aaa");
    token.setProtocolVersion((short) 2);
    token.setSerializer("ser");
    token.setDriverName("aa");
    token.setDriverVersion("aa");
    ByteArrayOutputStream bas = new ByteArrayOutputStream();
    ser.serialize(token, bas);
    ByteArrayInputStream input = new ByteArrayInputStream(bas.toByteArray());
    OBinaryToken tok = ser.deserialize(input);
    assertNull(token.getDatabase());
    assertNull(token.getDatabaseType());
    ORID id = token.getUserId();
    assertNull(id);
    assertEquals(20L, tok.getExpiry());
    assertTrue(token.isServerUser());
    assertEquals("aaa", tok.getUserName());
    assertEquals("OrientDB", tok.getHeader().getType());
    assertEquals("HmacSHA256", tok.getHeader().getAlgorithm());
    assertEquals("key", tok.getHeader().getKeyId());
    assertEquals((short) 2, tok.getProtocolVersion());
    assertEquals("ser", tok.getSerializer());
    assertEquals("aa", tok.getDriverName());
    assertEquals("aa", tok.getDriverVersion());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ORID(com.orientechnologies.orient.core.id.ORID) OBinaryToken(com.orientechnologies.orient.server.binary.impl.OBinaryToken) Test(org.junit.Test)

Aggregations

OBinaryToken (com.orientechnologies.orient.server.binary.impl.OBinaryToken)8 OSystemException (com.orientechnologies.common.exception.OSystemException)3 ORID (com.orientechnologies.orient.core.id.ORID)3 ORecordId (com.orientechnologies.orient.core.id.ORecordId)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Test (org.junit.Test)3 OTokenException (com.orientechnologies.orient.core.metadata.security.OTokenException)2 OException (com.orientechnologies.common.exception.OException)1 ONetworkProtocolData (com.orientechnologies.orient.server.network.protocol.ONetworkProtocolData)1 DataInputStream (java.io.DataInputStream)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1