Search in sources :

Example 11 with OSecurityAccessException

use of com.orientechnologies.orient.core.exception.OSecurityAccessException in project orientdb by orientechnologies.

the class OServerCommandAuthenticatedDbAbstract method getProfiledDatabaseInstanceBasic.

protected ODatabaseDocumentInternal getProfiledDatabaseInstanceBasic(final OHttpRequest iRequest) throws InterruptedException {
    final OHttpSession session = OHttpSessionManager.getInstance().getSession(iRequest.sessionId);
    if (session == null)
        throw new OSecurityAccessException(iRequest.databaseName, "No session active");
    // after authentication, if current login user is different compare with current DB user, reset DB user to login user
    ODatabaseDocumentInternal localDatabase = ODatabaseRecordThreadLocal.INSTANCE.getIfDefined();
    if (localDatabase == null) {
        localDatabase = (ODatabaseDocumentTx) server.openDatabase(iRequest.databaseName, session.getUserName(), session.getUserPassword());
    } else {
        String currentUserId = iRequest.data.currentUserId;
        if (currentUserId != null && currentUserId.length() > 0 && localDatabase != null && localDatabase.getUser() != null) {
            if (!currentUserId.equals(localDatabase.getUser().getIdentity().toString())) {
                ODocument userDoc = localDatabase.load(new ORecordId(currentUserId));
                localDatabase.setUser(new OUser(userDoc));
            }
        }
    }
    iRequest.data.lastDatabase = localDatabase.getName();
    iRequest.data.lastUser = localDatabase.getUser() != null ? localDatabase.getUser().getName() : null;
    return (ODatabaseDocumentTx) localDatabase.getDatabaseOwner();
}
Also used : OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OUser(com.orientechnologies.orient.core.metadata.security.OUser) ODatabaseDocumentInternal(com.orientechnologies.orient.core.db.ODatabaseDocumentInternal) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 12 with OSecurityAccessException

use of com.orientechnologies.orient.core.exception.OSecurityAccessException in project orientdb by orientechnologies.

the class OServerCommandPostAuthToken method execute.

@Override
public boolean execute(OHttpRequest iRequest, OHttpResponse iResponse) throws Exception {
    init();
    String[] urlParts = checkSyntax(iRequest.url, 2, "Syntax error: token/<database>");
    iRequest.databaseName = urlParts[1];
    iRequest.data.commandInfo = "Generate authentication token";
    // Parameter names consistent with 4.3.2 (Access Token Request) of RFC 6749
    Map<String, String> content = iRequest.getUrlEncodedContent();
    if (content == null) {
        ODocument result = new ODocument().field("error", "missing_auth_data");
        sendError(iRequest, iResponse, result);
        return false;
    }
    // signedJWT.serialize();
    String signedToken = "";
    String grantType = content.get("grant_type").toLowerCase();
    String username = content.get("username");
    String password = content.get("password");
    String authenticatedRid;
    ODocument result;
    if (grantType.equals("password")) {
        authenticatedRid = authenticate(username, password, iRequest.databaseName);
        if (authenticatedRid == null) {
            sendAuthorizationRequest(iRequest, iResponse, iRequest.databaseName);
        } else if (tokenHandler != null) {
            // Generate and return a JWT access token
            ODatabaseDocument db = null;
            OSecurityUser user = null;
            try {
                db = (ODatabaseDocument) server.openDatabase(iRequest.databaseName, username, password);
                user = db.getUser();
                if (user != null) {
                    byte[] tokenBytes = tokenHandler.getSignedWebToken(db, user);
                    signedToken = new String(tokenBytes);
                } else {
                // Server user (not supported yet!)
                }
            } catch (OSecurityAccessException e) {
            // WRONG USER/PASSWD
            } catch (OLockException e) {
                OLogManager.instance().error(this, "Cannot access to the database '" + iRequest.databaseName + "'", ODatabaseException.class, e);
            } finally {
                if (db != null) {
                    db.close();
                }
            }
            // 4.1.4 (Access Token Response) of RFC 6749
            result = new ODocument().field("access_token", signedToken).field("expires_in", 3600);
            iResponse.writeRecord(result, RESPONSE_FORMAT, null);
        } else {
            result = new ODocument().field("error", "unsupported_grant_type");
            sendError(iRequest, iResponse, result);
        }
    } else {
        result = new ODocument().field("error", "unsupported_grant_type");
        sendError(iRequest, iResponse, result);
    }
    return false;
}
Also used : OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OLockException(com.orientechnologies.common.concur.lock.OLockException) OSecurityUser(com.orientechnologies.orient.core.metadata.security.OSecurityUser) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 13 with OSecurityAccessException

use of com.orientechnologies.orient.core.exception.OSecurityAccessException in project orientdb by orientechnologies.

the class OServerCommandPostAuthToken method authenticate.

// Return user rid if authentication successful.
// If user is server user (doesn't have a rid) then '<server user>' is returned.
// null is returned in all other cases and means authentication was unsuccessful.
protected String authenticate(final String username, final String password, final String iDatabaseName) throws IOException {
    ODatabaseDocument db = null;
    String userRid = null;
    try {
        db = (ODatabaseDocument) server.openDatabase(iDatabaseName, username, password);
        userRid = (db.getUser() == null ? "<server user>" : db.getUser().getDocument().getIdentity().toString());
    } catch (OSecurityAccessException e) {
    // WRONG USER/PASSWD
    } catch (OLockException e) {
        OLogManager.instance().error(this, "Cannot access to the database '" + iDatabaseName + "'", ODatabaseException.class, e);
    } finally {
        if (db != null) {
            db.close();
        }
    }
    return userRid;
}
Also used : OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) OLockException(com.orientechnologies.common.concur.lock.OLockException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException)

Example 14 with OSecurityAccessException

use of com.orientechnologies.orient.core.exception.OSecurityAccessException in project orientdb by orientechnologies.

the class OServerCommandGetDatabase method exportClass.

public static void exportClass(final ODatabaseDocument db, final OJSONWriter json, final OClass cls) throws IOException {
    json.beginObject();
    json.writeAttribute("name", cls.getName());
    json.writeAttribute("superClass", cls.getSuperClass() != null ? cls.getSuperClass().getName() : "");
    json.beginCollection("superClasses");
    int i = 0;
    for (OClass oClass : cls.getSuperClasses()) {
        json.write((i > 0 ? "," : "") + "\"" + oClass.getName() + "\"");
        i++;
    }
    json.endCollection();
    json.writeAttribute("alias", cls.getShortName());
    json.writeAttribute("abstract", cls.isAbstract());
    json.writeAttribute("strictmode", cls.isStrictMode());
    json.writeAttribute("clusters", cls.getClusterIds());
    json.writeAttribute("defaultCluster", cls.getDefaultClusterId());
    json.writeAttribute("clusterSelection", cls.getClusterSelection().getName());
    if (cls instanceof OClassImpl) {
        final Map<String, String> custom = ((OClassImpl) cls).getCustomInternal();
        if (custom != null && !custom.isEmpty()) {
            json.writeAttribute("custom", custom);
        }
    }
    try {
        json.writeAttribute("records", db.countClass(cls.getName()));
    } catch (OSecurityAccessException e) {
        json.writeAttribute("records", "? (Unauthorized)");
    } catch (Exception e) {
        json.writeAttribute("records", "? (Error)");
    }
    if (cls.properties() != null && cls.properties().size() > 0) {
        json.beginCollection("properties");
        for (final OProperty prop : cls.properties()) {
            json.beginObject();
            json.writeAttribute("name", prop.getName());
            if (prop.getLinkedClass() != null)
                json.writeAttribute("linkedClass", prop.getLinkedClass().getName());
            if (prop.getLinkedType() != null)
                json.writeAttribute("linkedType", prop.getLinkedType().toString());
            json.writeAttribute("type", prop.getType().toString());
            json.writeAttribute("mandatory", prop.isMandatory());
            json.writeAttribute("readonly", prop.isReadonly());
            json.writeAttribute("notNull", prop.isNotNull());
            json.writeAttribute("min", prop.getMin());
            json.writeAttribute("max", prop.getMax());
            json.writeAttribute("regexp", prop.getRegexp());
            json.writeAttribute("collate", prop.getCollate() != null ? prop.getCollate().getName() : "default");
            json.writeAttribute("defaultValue", prop.getDefaultValue());
            if (prop instanceof OPropertyImpl) {
                final Map<String, String> custom = ((OPropertyImpl) prop).getCustomInternal();
                if (custom != null && !custom.isEmpty()) {
                    json.writeAttribute("custom", custom);
                }
            }
            json.endObject();
        }
        json.endCollection();
    }
    final Set<OIndex<?>> indexes = cls.getIndexes();
    if (!indexes.isEmpty()) {
        json.beginCollection("indexes");
        for (final OIndex<?> index : indexes) {
            json.beginObject();
            json.writeAttribute("name", index.getName());
            json.writeAttribute("type", index.getType());
            final OIndexDefinition indexDefinition = index.getDefinition();
            if (indexDefinition != null && !indexDefinition.getFields().isEmpty())
                json.writeAttribute("fields", indexDefinition.getFields());
            json.endObject();
        }
        json.endCollection();
    }
    json.endObject();
}
Also used : OProperty(com.orientechnologies.orient.core.metadata.schema.OProperty) OIndexDefinition(com.orientechnologies.orient.core.index.OIndexDefinition) OIndex(com.orientechnologies.orient.core.index.OIndex) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) OPropertyImpl(com.orientechnologies.orient.core.metadata.schema.OPropertyImpl) IOException(java.io.IOException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OSecurityAccessException(com.orientechnologies.orient.core.exception.OSecurityAccessException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Aggregations

OSecurityAccessException (com.orientechnologies.orient.core.exception.OSecurityAccessException)14 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)4 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)4 OLockException (com.orientechnologies.common.concur.lock.OLockException)3 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)2 ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)2 OIndex (com.orientechnologies.orient.core.index.OIndex)2 OIndexDefinition (com.orientechnologies.orient.core.index.OIndexDefinition)2 OProperty (com.orientechnologies.orient.core.metadata.schema.OProperty)2 OUser (com.orientechnologies.orient.core.metadata.security.OUser)2 OStorageProxy (com.orientechnologies.orient.core.storage.OStorageProxy)2 IOException (java.io.IOException)2 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)1 OException (com.orientechnologies.common.exception.OException)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1 OConfigurationException (com.orientechnologies.orient.core.exception.OConfigurationException)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OClassImpl (com.orientechnologies.orient.core.metadata.schema.OClassImpl)1 OPropertyImpl (com.orientechnologies.orient.core.metadata.schema.OPropertyImpl)1