Search in sources :

Example 1 with Connection

use of com.auth0.json.mgmt.Connection in project chemvantage by chuckwight.

the class Token method doGet.

// This servlet is the OpenID Connection starting point for platforms to reach ChemVantage
// The servlet identifies the deployment corresponding to the request, and returns a Java Web Token
// containing information needed for the subsequent launch request or other service request.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    StringBuffer debug = new StringBuffer("Issuing auth token:<br>");
    try {
        // store parameters required by third-party initiated login procedure:
        // this should be the platform_id URL (aud)
        String platform_id = request.getParameter("iss");
        debug.append("iss: " + platform_id + "<br>");
        String login_hint = request.getParameter("login_hint");
        debug.append("login_hint: " + login_hint + "<br>");
        String target_link_uri = request.getParameter("target_link_uri");
        debug.append("target_link_uri: " + target_link_uri + "<br>");
        debug.append("parameters: " + request.getParameterMap().keySet().toString() + "<br>");
        if (platform_id == null)
            throw new Exception("Missing required iss parameter.");
        if (login_hint == null)
            throw new Exception("Missing required login_hint parameter.");
        if (target_link_uri == null)
            throw new Exception("Missing required target_link_uri parameter.");
        String deployment_id = request.getParameter("lti_deployment_id");
        debug.append("deployment_id: " + deployment_id + "<br>");
        String client_id = request.getParameter("client_id");
        debug.append("client_id: " + client_id + "<br>");
        Deployment d = getDeployment(platform_id, deployment_id, client_id);
        if (d == null)
            throw new Exception("ChemVantage was unable to identify the deployment from your LMS. " + "Please check the registration to ensure the correct deployment_id and client_id. If your " + "platform registered multiple deployments with ChemVantage, it must provide the client_id " + "and/or deployment_id to uniquely identify one of them with each auth token request.<br/>" + "Contact admin@chemvantage.org for assistance.");
        String redirect_uri = target_link_uri;
        Date now = new Date();
        // 5 minutes from now
        Date exp = new Date(now.getTime() + 300000L);
        String nonce = Nonce.generateNonce();
        Algorithm algorithm = Algorithm.HMAC256(Subject.getHMAC256Secret());
        debug.append("JWT algorithm loaded OK.<br>");
        String iss = "https://" + request.getServerName();
        String token = JWT.create().withIssuer(iss).withSubject(login_hint).withAudience(platform_id).withExpiresAt(exp).withIssuedAt(now).withClaim("nonce", nonce).withClaim("deployment_id", d.getDeploymentId()).withClaim("client_id", d.client_id).withClaim("redirect_uri", redirect_uri).sign(algorithm);
        debug.append("JWT constructed and signed OK<br>");
        String lti_message_hint = request.getParameter("lti_message_hint");
        String oidc_auth_url = d.oidc_auth_url + "?response_type=id_token" + "&response_mode=form_post" + "&scope=openid" + "&prompt=none" + "&login_hint=" + login_hint + "&redirect_uri=" + redirect_uri + (lti_message_hint == null ? "" : "&lti_message_hint=" + lti_message_hint) + "&client_id=" + d.client_id + "&state=" + token + "&nonce=" + nonce;
        debug.append("Sending token: " + oidc_auth_url + "<p>");
        response.sendRedirect(oidc_auth_url);
    // d.claims = oidc_auth_url;
    // ofy().save().entity(d);
    } catch (Exception e) {
        response.getWriter().println("<h3>Failed Auth Token</h3>" + e.toString() + " " + e.getMessage() + "<br>" + debug.toString());
    }
}
Also used : Algorithm(com.auth0.jwt.algorithms.Algorithm) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) Date(java.util.Date)

Example 2 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class OrganizationsEntity method deleteConnection.

/**
 * Delete a connection from an organization. A token with {@code delete:organization_connections} scope is required.
 *
 * @param orgId the ID of the organization
 * @param connectionId the ID of the connection to delete
 * @return a Request to execute
 *
 * @see <a href="https://auth0.com/docs/api/management/v2#!/Organizations/delete_enabled_connections_by_connectionId">https://auth0.com/docs/api/management/v2#!/Organizations/delete_enabled_connections_by_connectionId</a>
 */
public Request<Void> deleteConnection(String orgId, String connectionId) {
    Asserts.assertNotNull(orgId, "organization ID");
    Asserts.assertNotNull(connectionId, "connection ID");
    String url = baseUrl.newBuilder().addPathSegments(ORGS_PATH).addPathSegment(orgId).addPathSegment("enabled_connections").addPathSegment(connectionId).build().toString();
    VoidRequest voidRequest = new VoidRequest(client, url, "DELETE");
    voidRequest.addHeader(AUTHORIZATION_HEADER, "Bearer " + apiToken);
    return voidRequest;
}
Also used : VoidRequest(com.auth0.net.VoidRequest)

Example 3 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class ConnectionsEntity method deleteUser.

/**
 * Delete an existing User from the given Database Connection. A token with scope delete:users is needed.
 * See https://auth0.com/docs/api/management/v2#!/Connections/delete_users_by_email
 *
 * @param connectionId the connection id where the user is stored.
 * @param email        the email of the user to delete.
 * @return a Request to execute.
 */
public Request<Void> deleteUser(String connectionId, String email) {
    Asserts.assertNotNull(connectionId, "connection id");
    Asserts.assertNotNull(email, "email");
    String url = baseUrl.newBuilder().addPathSegments("api/v2/connections").addPathSegment(connectionId).addPathSegment("users").addQueryParameter("email", email).build().toString();
    VoidRequest request = new VoidRequest(this.client, url, "DELETE");
    request.addHeader("Authorization", "Bearer " + apiToken);
    return request;
}
Also used : VoidRequest(com.auth0.net.VoidRequest)

Example 4 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class JobsEntityTest method shouldRequestUsersImport.

@Test
public void shouldRequestUsersImport() throws Exception {
    File usersFile = new File(MGMT_JOB_POST_USERS_IMPORTS_INPUT);
    Request<Job> request = api.jobs().importUsers("con_123456789", usersFile, null);
    assertThat(request, is(notNullValue()));
    server.jsonResponse(MGMT_JOB_POST_USERS_IMPORTS, 200);
    Job response = request.execute();
    RecordedRequest recordedRequest = server.takeRequest();
    assertThat(recordedRequest, hasMethodAndPath("POST", "/api/v2/jobs/users-imports"));
    assertThat(recordedRequest, hasHeader("Authorization", "Bearer apiToken"));
    String ctHeader = recordedRequest.getHeader("Content-Type");
    assertThat(ctHeader, startsWith("multipart/form-data"));
    String[] ctParts = ctHeader.split("multipart/form-data; boundary=");
    RecordedMultipartRequest recordedMultipartRequest = new RecordedMultipartRequest(recordedRequest);
    assertThat(recordedMultipartRequest.getPartsCount(), is(2));
    assertThat(recordedMultipartRequest.getBoundary(), is(notNullValue()));
    assertThat(recordedMultipartRequest.getBoundary(), is(ctParts[1]));
    // Connection ID
    KeyValuePart formParam = recordedMultipartRequest.getKeyValuePart("connection_id");
    assertThat(formParam, is(notNullValue()));
    assertThat(formParam.getValue(), is("con_123456789"));
    // Users JSON
    FilePart jsonFile = recordedMultipartRequest.getFilePart("users");
    assertThat(jsonFile, is(notNullValue()));
    String utf8Contents = new String(Files.readAllBytes(usersFile.toPath()));
    assertThat(jsonFile.getContentType(), is("text/json"));
    assertThat(jsonFile.getFilename(), is("job_post_users_imports_input.json"));
    assertThat(jsonFile.getValue(), is(utf8Contents));
    assertThat(response, is(notNullValue()));
}
Also used : RecordedRequest(okhttp3.mockwebserver.RecordedRequest) RecordedMultipartRequest(com.auth0.net.multipart.RecordedMultipartRequest) Job(com.auth0.json.mgmt.jobs.Job) File(java.io.File) KeyValuePart(com.auth0.net.multipart.KeyValuePart) FilePart(com.auth0.net.multipart.FilePart) Test(org.junit.Test)

Example 5 with Connection

use of com.auth0.json.mgmt.Connection in project auth0-java by auth0.

the class JobsEntityTest method shouldThrowOnRequestUsersImportWithNullConnectionId.

@Test
public void shouldThrowOnRequestUsersImportWithNullConnectionId() {
    exception.expect(IllegalArgumentException.class);
    exception.expectMessage("'connection id' cannot be null!");
    File usersFile = mock(File.class);
    when(usersFile.exists()).thenReturn(true);
    UsersImportOptions options = mock(UsersImportOptions.class);
    api.jobs().importUsers(null, usersFile, options);
}
Also used : UsersImportOptions(com.auth0.client.mgmt.filter.UsersImportOptions) File(java.io.File) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)16 RecordedRequest (okhttp3.mockwebserver.RecordedRequest)13 Connection (com.auth0.json.mgmt.Connection)10 List (java.util.List)9 IOException (java.io.IOException)6 ConnectionFilter (com.auth0.client.mgmt.filter.ConnectionFilter)5 Auth0Exception (com.auth0.exception.Auth0Exception)5 APIException (com.auth0.exception.APIException)4 RateLimitException (com.auth0.exception.RateLimitException)4 RecordedMultipartRequest (com.auth0.net.multipart.RecordedMultipartRequest)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)4 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)4 ExpectedException (org.junit.rules.ExpectedException)4 VoidRequest (com.auth0.net.VoidRequest)3 File (java.io.File)3 UsersImportOptions (com.auth0.client.mgmt.filter.UsersImportOptions)2 Job (com.auth0.json.mgmt.jobs.Job)2 User (com.auth0.json.mgmt.users.User)2 FilePart (com.auth0.net.multipart.FilePart)2 KeyValuePart (com.auth0.net.multipart.KeyValuePart)2