Search in sources :

Example 1 with ApiClient

use of cbit.vcell.modeldb.ApiClient in project vcell by virtualcell.

the class VCellCookieAuthenticator method login.

@Override
protected void login(Request request, Response response) {
    // Login detected
    Representation entity = request.getEntity();
    Form form = new Form(entity);
    Parameter identifier = form.getFirst(getIdentifierFormName());
    Parameter secret = form.getFirst(getSecretFormName());
    Parameter redirectURL = form.getFirst(getRedirectQueryName());
    UserLoginInfo.DigestedPassword digestedPassword = new UserLoginInfo.DigestedPassword(secret.getValue());
    try {
        User user = vcellApiApplication.getUserVerifier().authenticateUser(identifier.getValue(), digestedPassword.getString().toCharArray());
        if (user == null) {
            response.setStatus(Status.CLIENT_ERROR_UNAUTHORIZED);
            return;
        }
        ApiClient apiClient = vcellApiApplication.getUserVerifier().getApiClient(VCellApiApplication.BROWSER_CLIENTID);
        ApiAccessToken accessToken = vcellApiApplication.getUserVerifier().generateApiAccessToken(apiClient.getKey(), user);
        // Set credentials
        ChallengeResponse cr = new ChallengeResponse(getScheme(), CustomAuthHelper.ACCESS_TOKEN, accessToken.getToken());
        request.setChallengeResponse(cr);
        getCredentialsCookie(request, response).setMaxAge(0);
        getLogger().log(Level.INFO, "MyCookieAuthenticator.login(request,response) - created new accessToken '" + accessToken.getToken() + "' and assignd to ChallengeResponse, redirectURL='" + redirectURL.getValue() + "'");
        response.redirectSeeOther(Reference.decode(redirectURL.getValue()));
    } catch (SQLException e) {
        e.printStackTrace();
        getLogger().log(Level.SEVERE, "MyCookieAuthenticator.login(request,response) - exception", e);
    } catch (DataAccessException e) {
        e.printStackTrace();
        getLogger().log(Level.SEVERE, "MyCookieAuthenticator.login(request,response) - exception", e);
    }
}
Also used : User(org.vcell.util.document.User) Form(org.restlet.data.Form) SQLException(java.sql.SQLException) ApiAccessToken(cbit.vcell.modeldb.ApiAccessToken) Parameter(org.restlet.data.Parameter) Representation(org.restlet.representation.Representation) UserLoginInfo(org.vcell.util.document.UserLoginInfo) ApiClient(cbit.vcell.modeldb.ApiClient) DataAccessException(org.vcell.util.DataAccessException) ChallengeResponse(org.restlet.data.ChallengeResponse)

Example 2 with ApiClient

use of cbit.vcell.modeldb.ApiClient in project vcell by virtualcell.

the class AccessTokenServerResource method get_json.

@Override
public AccessTokenRepresentation get_json() {
    VCellApiApplication application = ((VCellApiApplication) getApplication());
    String clientId = getQueryValue(PARAM_CLIENT_ID);
    String userId = getQueryValue(PARAM_USER_ID);
    String userPassword = getQueryValue(PARAM_USER_PASSWORD);
    try {
        ApiClient apiClient = application.getUserVerifier().getApiClient(clientId);
        if (apiClient == null) {
            throw new RuntimeException("client not found");
        }
        User authenticatedUser = application.getUserVerifier().authenticateUser(userId, userPassword.toCharArray());
        if (authenticatedUser == null) {
            throw new RuntimeException("unable to authenticate user");
        }
        ApiAccessToken apiAccessToken = application.getUserVerifier().generateApiAccessToken(apiClient.getKey(), authenticatedUser);
        AccessTokenRepresentation tokenRep = new AccessTokenRepresentation(apiAccessToken);
        // 
        // indicate no caching of response.
        // 
        ArrayList<CacheDirective> cacheDirectives = new ArrayList<CacheDirective>();
        cacheDirectives.add(CacheDirective.noCache());
        getResponse().setCacheDirectives(cacheDirectives);
        return tokenRep;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : User(org.vcell.util.document.User) CacheDirective(org.restlet.data.CacheDirective) ApiAccessToken(cbit.vcell.modeldb.ApiAccessToken) ArrayList(java.util.ArrayList) VCellApiApplication(org.vcell.rest.VCellApiApplication) ApiClient(cbit.vcell.modeldb.ApiClient) AccessTokenRepresentation(org.vcell.rest.common.AccessTokenRepresentation) ResourceException(org.restlet.resource.ResourceException)

Aggregations

ApiAccessToken (cbit.vcell.modeldb.ApiAccessToken)2 ApiClient (cbit.vcell.modeldb.ApiClient)2 User (org.vcell.util.document.User)2 SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 CacheDirective (org.restlet.data.CacheDirective)1 ChallengeResponse (org.restlet.data.ChallengeResponse)1 Form (org.restlet.data.Form)1 Parameter (org.restlet.data.Parameter)1 Representation (org.restlet.representation.Representation)1 ResourceException (org.restlet.resource.ResourceException)1 VCellApiApplication (org.vcell.rest.VCellApiApplication)1 AccessTokenRepresentation (org.vcell.rest.common.AccessTokenRepresentation)1 DataAccessException (org.vcell.util.DataAccessException)1 UserLoginInfo (org.vcell.util.document.UserLoginInfo)1