Search in sources :

Example 6 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class OrientDBDao method query.

@Override
public List<TransientObject> query(Query query) throws DAOException {
    checkDb();
    List<TransientObject> list = new ArrayList<TransientObject>();
    OTransaction transaction = db.getTransaction();
    transaction.begin();
    try {
        String q = query.getSQL();
        System.out.println("OrientDB_Query: " + q);
        if (query.getAction().equals(QueryBuilder.QueryAction.SELECT)) {
            List<ODocument> objects = db.query(new OSQLSynchQuery<ODocument>(q));
            for (ODocument w : objects) {
                list.add(new ODocumentWrapper(w).toObject(TransientObject.class));
            }
        }
        if (query.getAction().equals(QueryBuilder.QueryAction.DELETE)) {
            //            List<ODocument> objects = db.command(new OCommandSQL("delete from io.divide.dao.TestObject1 RETURN BEFORE")).execute();
            Integer objects = db.command(new OCommandSQL(q)).execute();
            TransientObject o = new EmptyTO();
            o.put("count", objects);
            list.add(o);
            System.out.println("Delete: " + objects);
        }
        transaction.commit();
        transaction.close();
    } catch (Exception e) {
        transaction.rollback();
        transaction.close();
    }
    return list;
}
Also used : ArrayList(java.util.ArrayList) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OTransaction(com.orientechnologies.orient.core.tx.OTransaction) TransientObject(io.divide.shared.transitory.TransientObject) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 7 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class SecurityFilter method authenticate.

private UserContext authenticate(ContainerRequestContext request) {
    // Extract authentication credentials
    String authentication = request.getHeaderString(ContainerRequest.AUTHORIZATION);
    System.out.println("HeaderCount: " + request.getHeaders().keySet().size());
    System.out.println(request.getHeaders().keySet());
    System.out.println(request.getPropertyNames());
    System.out.println(request.getCookies().keySet());
    if (authentication == null) {
        return abort(request, "Authentication credentials are required");
    }
    if (!authentication.startsWith("CUSTOM ")) {
        return abort(request, "Only CUSTOM authentication is supported: " + authentication);
    }
    authentication = authentication.substring("CUSTOM ".length());
    String token = authentication;
    if (token == null) {
        return abort(request, "Missing token");
    }
    // TODO verify
    try {
        AuthTokenUtils.AuthToken authToken = new AuthTokenUtils.AuthToken(securityManager.getSymmetricKey(), token);
        if (authToken.isExpired()) {
            return abort(request, "Auth Token Expired: " + System.currentTimeMillis() + " : " + authToken.expirationDate);
        }
    } catch (AuthTokenUtils.AuthenticationException e) {
        return abort(request, "Auth Token Expired: " + e.getMessage());
    }
    // Validate the extracted credentials
    synchronized (dao) {
        Query q = new QueryBuilder().select().from(Credentials.class).where(Credentials.AUTH_TOKEN_KEY, OPERAND.EQ, token).build();
        try {
            TransientObject temp = ObjectUtils.get1stOrNull(dao.query(q));
            if (temp != null) {
                ServerCredentials creds = new ServerCredentials(temp);
                //                    creds.setAuthToken(AuthTokenUtils.getNewToken(securityManager.getSymmetricKey(),creds)); // assign new token
                return new UserContext(request.getUriInfo(), new ServerCredentials(creds));
            } else {
                //                    System.err.println("IN DB: " + dao.query(new QueryBuilder().select().from(Credentials.class).build()));
                request.abortWith(notAuthReponse("Invalid authentication token"));
                return abort(request, "Invalid authentication token");
            }
        } catch (ServerDAO.DAOException e) {
            log.severe("Authentication Failed(" + e.getStatusCode() + ") " + e.getMessage());
            e.printStackTrace();
            return abort(request, "Invalid authentication token");
        }
    }
}
Also used : ServerDAO(io.divide.dao.ServerDAO) Query(io.divide.shared.transitory.query.Query) ServerCredentials(io.divide.server.dao.ServerCredentials) AuthTokenUtils(io.divide.shared.util.AuthTokenUtils) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) TransientObject(io.divide.shared.transitory.TransientObject)

Example 8 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class PushEndpointTest method testRegister.

@Test
public void testRegister() throws Exception {
    Credentials user = AuthenticationEndpointTest.signUpUser(this);
    PublicKey key = AuthenticationEndpointTest.getPublicKey(this);
    registerToken(user, key, this);
    Collection<TransientObject> list = container.serverDao.query(new QueryBuilder().select().from(Credentials.class).build());
    TransientObject o = ObjectUtils.get1stOrNull(list);
    user = TestUtils.convert(o, Credentials.class);
    assertNotNull(user);
    // check the token was actually saved
    assertEquals("whatwhat", user.getPushMessagingKey());
}
Also used : PublicKey(java.security.PublicKey) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) TransientObject(io.divide.shared.transitory.TransientObject) Credentials(io.divide.shared.transitory.Credentials) ServerTest(io.divide.server.ServerTest) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 9 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class AuthServerLogic method getUserFromAuthToken.

public Credentials getUserFromAuthToken(String token) throws DAOException {
    AuthTokenUtils.AuthToken authToken;
    try {
        authToken = new AuthTokenUtils.AuthToken(keyManager.getSymmetricKey(), token);
    } catch (AuthenticationException e) {
        throw new DAOException(HttpStatus.SC_INTERNAL_SERVER_ERROR, "internal error");
    }
    if (authToken.isExpired())
        throw new DAOException(HttpStatus.SC_UNAUTHORIZED, "Expired");
    Query q = new QueryBuilder().select().from(Credentials.class).where(Credentials.AUTH_TOKEN_KEY, OPERAND.EQ, token).build();
    TransientObject to = ObjectUtils.get1stOrNull(dao.query(q));
    if (to != null) {
        return new ServerCredentials(to);
    } else {
        throw new DAOException(HttpStatus.SC_BAD_REQUEST, "invalid auth token");
    }
}
Also used : DAOException(io.divide.shared.server.DAO.DAOException) Query(io.divide.shared.transitory.query.Query) AuthenticationException(io.divide.shared.util.AuthTokenUtils.AuthenticationException) AuthTokenUtils(io.divide.shared.util.AuthTokenUtils) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) TransientObject(io.divide.shared.transitory.TransientObject)

Example 10 with TransientObject

use of io.divide.shared.transitory.TransientObject in project divide by HiddenStage.

the class DaoUtils method getUserByEmail.

/**
     * Convience method to do a Credentials query against an email address
     * @param serverDao dao object to query against.
     * @param email email address used in query.
     * @return Credentials object found or null.
     * @throws io.divide.shared.server.DAO.DAOException
     */
public static Credentials getUserByEmail(DAO serverDao, String email) throws DAO.DAOException {
    Query query = new QueryBuilder().select().from(Credentials.class).where(Credentials.EMAIL_KEY, OPERAND.EQ, email).build();
    TransientObject to = (TransientObject) ObjectUtils.get1stOrNull(serverDao.query(query));
    if (to == null) {
        return null;
    } else {
        return to(Credentials.class, to);
    }
}
Also used : Query(io.divide.shared.transitory.query.Query) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) TransientObject(io.divide.shared.transitory.TransientObject)

Aggregations

TransientObject (io.divide.shared.transitory.TransientObject)21 QueryBuilder (io.divide.shared.transitory.query.QueryBuilder)10 Query (io.divide.shared.transitory.query.Query)9 Test (org.junit.Test)6 OTransaction (com.orientechnologies.orient.core.tx.OTransaction)3 NotImplementedException (sun.reflect.generics.reflectiveObjects.NotImplementedException)3 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)2 ServerTest (io.divide.server.ServerTest)2 DAOException (io.divide.shared.server.DAO.DAOException)2 Credentials (io.divide.shared.transitory.Credentials)2 AuthTokenUtils (io.divide.shared.util.AuthTokenUtils)2 PublicKey (java.security.PublicKey)2 ArrayList (java.util.ArrayList)2 JerseyTest (org.glassfish.jersey.test.JerseyTest)2 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 TypeLiteral (com.google.inject.TypeLiteral)1 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)1 CommitExpection (iBoxDB.LocalServer.E.CommitExpection)1 MockKeyManager (io.divide.client.auth.MockKeyManager)1