Search in sources :

Example 1 with BearerTokenMetadata

use of org.kin.rsocket.core.metadata.BearerTokenMetadata in project kin-rsocket-broker by huangjianqin.

the class RSocketServiceManager method acceptor.

/**
 * service rsocket endpoint acceptor逻辑
 */
@SuppressWarnings("ConstantConditions")
@Nonnull
private Mono<RSocket> acceptor(ConnectionSetupPayload setupPayload, RSocket requester) {
    // parse setup payload
    RSocketCompositeMetadata compositeMetadata = null;
    AppMetadata appMetadata = null;
    String credentials = "";
    RSocketAppPrincipal principal = null;
    String errorMsg = null;
    try {
        compositeMetadata = RSocketCompositeMetadata.of(setupPayload.metadata());
        if (!authRequired) {
            // authentication not required
            principal = RSocketAppPrincipal.DEFAULT;
            credentials = UUID.randomUUID().toString();
        } else if (compositeMetadata.contains(RSocketMimeType.BEARER_TOKEN)) {
            BearerTokenMetadata bearerTokenMetadata = compositeMetadata.getMetadata(RSocketMimeType.BEARER_TOKEN);
            credentials = new String(bearerTokenMetadata.getBearerToken());
            principal = authenticationService.auth(credentials);
        } else {
            // no jwt token supplied
            errorMsg = "Failed to accept the connection, please check app info and JWT token";
        }
        // validate application information
        if (principal != null && compositeMetadata.contains(RSocketMimeType.APPLICATION)) {
            AppMetadata temp = compositeMetadata.getMetadata(RSocketMimeType.APPLICATION);
            // App registration validation: app id: UUID and unique in server
            String appId = temp.getUuid();
            // validate appId data format
            if (StringUtils.isNotBlank(appId) && appId.length() >= 32) {
                int instanceId = MurmurHash3.hash32(credentials + ":" + temp.getUuid());
                temp.updateInstanceId(instanceId);
                // application instance not connected
                if (!containsInstanceId(instanceId)) {
                    appMetadata = temp;
                    appMetadata.updateConnectedAt(new Date());
                } else {
                    // application connected already
                    errorMsg = "Connection created already, Please don't create multiple connections.";
                }
            } else {
                // 没有uuid是否要拒绝连接
                // illegal application id, appID should be UUID
                errorMsg = String.format("'%s' is not legal application ID, please supply legal UUID as Application ID", appId == null ? "" : appId);
            }
        } else {
            errorMsg = "Can not found application metadata";
        }
        if (errorMsg == null) {
            // Security authentication
            if (appMetadata != null) {
                appMetadata.addMetadata("_orgs", String.join(",", principal.getOrganizations()));
                appMetadata.addMetadata("_roles", String.join(",", principal.getRoles()));
                appMetadata.addMetadata("_serviceAccounts", String.join(",", principal.getServiceAccounts()));
            } else {
                errorMsg = "Please supply message/x.rsocket.application+json metadata in setup payload";
            }
        }
    } catch (Exception e) {
        log.error("Error to parse setup payload", e);
        errorMsg = String.format("Failed to parse composite metadata: %s", e.getMessage());
    }
    // validate connection legal or not
    if (principal == null) {
        errorMsg = "Failed to accept the connection, please check app info and JWT token";
    }
    if (errorMsg != null) {
        return returnRejectedRSocket(errorMsg, requester);
    }
    // create rsocket endpoint
    try {
        RSocketBrokerResponderHandler responderHandler = new RSocketBrokerResponderHandler(setupPayload, appMetadata, principal, this, serviceMeshInspector, upstreamBrokers, rsocketFilterChain);
        RSocketEndpoint rsocketEndpoint = new RSocketEndpoint(compositeMetadata, appMetadata, requester, this, responderHandler);
        rsocketEndpoint.onClose().doOnTerminate(() -> onRSocketEndpointDisposed(rsocketEndpoint)).subscribeOn(Schedulers.parallel()).subscribe();
        // handler registration notify
        registerRSocketEndpoint(rsocketEndpoint);
        // connect success, so publish service now
        rsocketEndpoint.publishServices();
        log.info(String.format("succeed to accept connection from application '%s'", appMetadata.getName()));
        return Mono.just(responderHandler);
    } catch (Exception e) {
        String formattedErrorMsg = String.format("failed to accept the connection: %s", e.getMessage());
        log.error(formattedErrorMsg, e);
        return returnRejectedRSocket(formattedErrorMsg, requester);
    }
}
Also used : RSocketCompositeMetadata(org.kin.rsocket.core.metadata.RSocketCompositeMetadata) BearerTokenMetadata(org.kin.rsocket.core.metadata.BearerTokenMetadata) AppMetadata(org.kin.rsocket.core.metadata.AppMetadata) RSocketAppPrincipal(org.kin.rsocket.auth.RSocketAppPrincipal) RejectedSetupException(io.rsocket.exceptions.RejectedSetupException) ApplicationErrorException(io.rsocket.exceptions.ApplicationErrorException) Nonnull(javax.annotation.Nonnull)

Aggregations

ApplicationErrorException (io.rsocket.exceptions.ApplicationErrorException)1 RejectedSetupException (io.rsocket.exceptions.RejectedSetupException)1 Nonnull (javax.annotation.Nonnull)1 RSocketAppPrincipal (org.kin.rsocket.auth.RSocketAppPrincipal)1 AppMetadata (org.kin.rsocket.core.metadata.AppMetadata)1 BearerTokenMetadata (org.kin.rsocket.core.metadata.BearerTokenMetadata)1 RSocketCompositeMetadata (org.kin.rsocket.core.metadata.RSocketCompositeMetadata)1