Search in sources :

Example 1 with UiSessionToken

use of org.onosproject.ui.UiSessionToken in project onos by opennetworkinglab.

the class MainIndexResource method getMainIndex.

@GET
@Produces(MediaType.TEXT_HTML)
@Path("/index.html")
public Response getMainIndex() throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    UiExtensionService service;
    UiTokenService tokens;
    try {
        service = get(UiExtensionService.class);
        tokens = get(UiTokenService.class);
    } catch (ServiceNotFoundException e) {
        return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build();
    }
    InputStream indexTemplate = classLoader.getResourceAsStream(INDEX);
    String index = new String(toByteArray(indexTemplate));
    int p0s = split(index, 0, INJECT_USER_START) - INJECT_USER_START.length();
    int p0e = split(index, p0s, INJECT_USER_END);
    int p1s = split(index, p0e, INJECT_JS_START) - INJECT_JS_START.length();
    int p1e = split(index, p1s, INJECT_JS_END);
    int p2s = split(index, p1e, INJECT_CSS_START) - INJECT_CSS_START.length();
    int p2e = split(index, p2s, INJECT_CSS_END);
    int p3s = split(index, p2e, null);
    // FIXME: use global opaque auth token to allow secure failover
    // for now, just use the user principal name...
    String userName = ctx.getUserPrincipal().getName();
    // get a session token to use for UI-web-socket authentication
    UiSessionToken token = tokens.issueToken(userName);
    String auth = "var onosUser='" + userName + "',\n" + "    onosAuth='" + token + "';\n";
    StreamEnumeration streams = new StreamEnumeration(of(stream(index, 0, p0s), new ByteArrayInputStream(SCRIPT_START), stream(auth, 0, auth.length()), userPreferences(userName), userConsoleLog(userName), new ByteArrayInputStream(SCRIPT_END), stream(index, p0e, p1s), includeJs(service), stream(index, p1e, p2s), includeCss(service), stream(index, p2e, p3s)));
    return Response.ok(new SequenceInputStream(streams)).build();
}
Also used : SequenceInputStream(java.io.SequenceInputStream) UiTokenService(org.onosproject.ui.UiTokenService) ByteArrayInputStream(java.io.ByteArrayInputStream) ServiceNotFoundException(org.onlab.osgi.ServiceNotFoundException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UiExtensionService(org.onosproject.ui.UiExtensionService) UiSessionToken(org.onosproject.ui.UiSessionToken) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with UiSessionToken

use of org.onosproject.ui.UiSessionToken in project onos by opennetworkinglab.

the class MainIndexResource method getMainIndex.

@GET
@Produces(MediaType.TEXT_HTML)
@Path("/index.html")
public Response getMainIndex() throws IOException {
    ClassLoader classLoader = getClass().getClassLoader();
    UiExtensionService service;
    UiTokenService tokens;
    try {
        service = get(UiExtensionService.class);
        tokens = get(UiTokenService.class);
    } catch (ServiceNotFoundException e) {
        return Response.ok(classLoader.getResourceAsStream(NOT_READY)).build();
    }
    InputStream indexTemplate = classLoader.getResourceAsStream(INDEX);
    String index = new String(toByteArray(indexTemplate));
    int p0s = split(index, 0, INJECT_USER_START) - INJECT_USER_START.length();
    int p0e = split(index, p0s, INJECT_USER_END);
    int p3s = split(index, p0e, null);
    // FIXME: use global opaque auth token to allow secure failover
    // for now, just use the user principal name...
    String userName = ctx.getUserPrincipal().getName();
    // get a session token to use for UI-web-socket authentication
    UiSessionToken token = tokens.issueToken(userName);
    String auth = "var onosUser='" + userName + "',\n" + "    onosAuth='" + token + "';\n";
    StreamEnumeration streams = new StreamEnumeration(of(stream(index, 0, p0s), new ByteArrayInputStream(SCRIPT_START), stream(auth, 0, auth.length()), userPreferences(userName), userConsoleLog(userName), new ByteArrayInputStream(SCRIPT_END), stream(index, p0e, p3s)));
    return Response.ok(new SequenceInputStream(streams)).build();
}
Also used : SequenceInputStream(java.io.SequenceInputStream) UiTokenService(org.onosproject.ui.UiTokenService) ByteArrayInputStream(java.io.ByteArrayInputStream) ServiceNotFoundException(org.onlab.osgi.ServiceNotFoundException) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) UiExtensionService(org.onosproject.ui.UiExtensionService) UiSessionToken(org.onosproject.ui.UiSessionToken) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with UiSessionToken

use of org.onosproject.ui.UiSessionToken in project onos by opennetworkinglab.

the class UiWebSocket method authenticate.

private void authenticate(String type, ObjectNode message) {
    if (!AUTHENTICATION.equals(type)) {
        log.warn("WebSocket not authenticated: {}", message);
        sendMessage(ERROR, notAuthorized(null));
        close();
        return;
    }
    String tokstr = message.path(PAYLOAD).path(TOKEN).asText(UNKNOWN);
    UiSessionToken token = new UiSessionToken(tokstr);
    if (tokenService().isTokenValid(token)) {
        sessionToken = token;
        log.info("Session token authenticated");
        log.debug("WebSocket authenticated: {}", message);
    } else {
        log.warn("Invalid Authentication Token: {}", message);
        sendMessage(ERROR, notAuthorized(token));
    }
}
Also used : UiSessionToken(org.onosproject.ui.UiSessionToken)

Example 4 with UiSessionToken

use of org.onosproject.ui.UiSessionToken in project onos by opennetworkinglab.

the class UiExtensionManager method issueToken.

// =====================================================================
// UiTokenService
@Override
public UiSessionToken issueToken(String username) {
    UiSessionToken token = new UiSessionToken(tokenGen.nextSessionId());
    tokens.put(token, username);
    log.debug("UiSessionToken issued: {}", token);
    return token;
}
Also used : UiSessionToken(org.onosproject.ui.UiSessionToken)

Aggregations

UiSessionToken (org.onosproject.ui.UiSessionToken)4 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 SequenceInputStream (java.io.SequenceInputStream)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 ServiceNotFoundException (org.onlab.osgi.ServiceNotFoundException)2 UiExtensionService (org.onosproject.ui.UiExtensionService)2 UiTokenService (org.onosproject.ui.UiTokenService)2