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;
}
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);
}
}
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;
}
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());
}
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());
}
Aggregations