Java Code Examples for com.hazelcast.core.IMap#set()

The following examples show how to use com.hazelcast.core.IMap#set() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Oauth2UserPutHandler.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    User user = Config.getInstance().getMapper().convertValue(body, User.class);
    String userId = user.getUserId();
    IMap<String, User> users = CacheStartupHookProvider.hz.getMap("users");
    User u = users.get(userId);
    if(u == null) {
        setExchangeStatus(exchange, USER_NOT_FOUND, userId);
    } else {
        // as password is not in the return value, chances are password is not in the user object
        user.setPassword(u.getPassword());
        users.set(userId, user);
    }
    processAudit(exchange);
}
 
Example 2
Source File: Oauth2ServicePutHandler.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    Service service = Config.getInstance().getMapper().convertValue(body, Service.class);

    String serviceId = service.getServiceId();

    IMap<String, Service> services = CacheStartupHookProvider.hz.getMap("services");
    if(services.get(serviceId) == null) {
        setExchangeStatus(exchange, SERVICE_NOT_FOUND, serviceId);
    } else {
        services.set(serviceId, service);
    }
    processAudit(exchange);
}
 
Example 3
Source File: Oauth2ServiceServiceIdEndpointPostHandler.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    List<Map<String, Object>> body = (List)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    String serviceId = exchange.getQueryParameters().get("serviceId").getFirst();
    if(logger.isDebugEnabled()) logger.debug("post serviceEndpoints for serviceId " + serviceId);

    // ensure that the serviceId exists
    IMap<String, Service> services = CacheStartupHookProvider.hz.getMap("services");
    if(services.get(serviceId) == null) {
        setExchangeStatus(exchange, SERVICE_NOT_FOUND, serviceId);
        processAudit(exchange);
        return;
    }

    IMap<String, List<ServiceEndpoint>> serviceEndpoints = CacheStartupHookProvider.hz.getMap("serviceEndpoints");
    List<ServiceEndpoint> list = new ArrayList<>();
    for(Map<String, Object> m: body) {
        list.add(Config.getInstance().getMapper().convertValue(m, ServiceEndpoint.class));
    }
    serviceEndpoints.set(serviceId, list);
    processAudit(exchange);
}
 
Example 4
Source File: Oauth2ClientPutHandler.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    Client client = Config.getInstance().getMapper().convertValue(body, Client.class);
    if(client.getDerefClientId() != null && Client.ClientTypeEnum.EXTERNAL != client.getClientType()) {
        // only external client may have deref client id
        setExchangeStatus(exchange, DEREF_NOT_EXTERNAL);
        return;
    }

    String clientId = client.getClientId();

    IMap<String, Client> clients = CacheStartupHookProvider.hz.getMap("clients");
    Client originalClient = clients.get(clientId);
    if(originalClient == null) {
        setExchangeStatus(exchange, CLIENT_NOT_FOUND, clientId);
    } else {
        // set client secret as it is not returned by query.
        client.setClientSecret(originalClient.getClientSecret());
        clients.set(clientId, client);
    }
    processAudit(exchange);
}
 
Example 5
Source File: Oauth2ProviderPostHandler.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map<String, Object>)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    Provider provider = Config.getInstance().getMapper().convertValue(body, Provider.class);

    String provider_id = provider.getProviderId() ;

    IMap<String, Provider> providers = CacheStartupHookProvider.hz.getMap("providers");
    if(providers.get(provider_id) == null) {
        providers.set(provider_id, provider);
        exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(provider));
    } else {
        setExchangeStatus(exchange, PROVIDER_ID_EXISTS, provider_id);
    }
    processAudit(exchange);
}
 
Example 6
Source File: Oauth2ProviderPutHandler.java    From light-oauth2 with Apache License 2.0 6 votes vote down vote up
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {

    Map<String, Object> body = (Map<String, Object>)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    Provider provider = Config.getInstance().getMapper().convertValue(body, Provider.class);

    String provider_id = provider.getProviderId() ;

    IMap<String, Provider> providers = CacheStartupHookProvider.hz.getMap("providers");
    if(providers.get(provider_id) == null) {
        setExchangeStatus(exchange, PROVIDER_ID_INVALID);
    } else {
        providers.set(provider_id, provider);
        exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(provider));
    }
    processAudit(exchange);
    
}
 
Example 7
Source File: Oauth2PasswordUserIdPostHandler.java    From light-oauth2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    String userId = exchange.getQueryParameters().get("userId").getFirst();
    char[] password = null;
    if(body.get("password") != null) {
        password = ((String)body.get("password")).toCharArray();
    }
    String newPassword = (String)body.get("newPassword");
    String newPasswordConfirm = (String)body.get("newPasswordConfirm");

    IMap<String, User> users = CacheStartupHookProvider.hz.getMap("users");
    User user = users.get(userId);
    if(user == null) {
        setExchangeStatus(exchange, USER_NOT_FOUND, userId);
        processAudit(exchange);
    } else {
        if(!HashUtil.validatePassword(password, user.getPassword())) {
            setExchangeStatus(exchange, INCORRECT_PASSWORD);
            processAudit(exchange);
            return;
        }
        if(newPassword.equals(newPasswordConfirm)) {
            String hashedPass = HashUtil.generateStrongPasswordHash(newPassword);
            user.setPassword(hashedPass);
            users.set(userId, user);
        } else {
            setExchangeStatus(exchange, PASSWORD_PASSWORDCONFIRM_NOT_MATCH, newPassword, newPasswordConfirm);
        }
        processAudit(exchange);
    }
}
 
Example 8
Source File: Oauth2ServicePostHandler.java    From light-oauth2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    Service service = Config.getInstance().getMapper().convertValue(body, Service.class);

    String serviceId = service.getServiceId();
    IMap<String, Service> services = CacheStartupHookProvider.hz.getMap("services");
    if(services.get(serviceId) == null) {
        services.set(serviceId, service);
    } else {
        setExchangeStatus(exchange, SERVICE_ID_EXISTS, serviceId);
    }
    processAudit(exchange);
}
 
Example 9
Source File: Oauth2ClientPostHandler.java    From light-oauth2 with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map<String, Object>)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    Client client = Config.getInstance().getMapper().convertValue(body, Client.class);
    if(client.getDerefClientId() != null && Client.ClientTypeEnum.EXTERNAL != client.getClientType()) {
        // only external client may have deref client id
        setExchangeStatus(exchange, DEREF_NOT_EXTERNAL);
        return;
    }
    // generate client_id and client_secret here.
    String clientId = UUID.randomUUID().toString();
    client.setClientId(clientId);
    String clientSecret = Util.getUUID();
    client.setClientSecret(HashUtil.generateStrongPasswordHash(clientSecret));

    IMap<String, Client> clients = CacheStartupHookProvider.hz.getMap("clients");
    if(clients.get(clientId) == null) {
        clients.set(clientId, client);
        // send the client back with client_id and client_secret
        Client c = Client.copyClient(client);
        c.setClientSecret(clientSecret);
        exchange.getResponseSender().send(Config.getInstance().getMapper().writeValueAsString(c));
    } else {
        setExchangeStatus(exchange, CLIENT_ID_EXISTS, clientId);
    }
    processAudit(exchange);
}
 
Example 10
Source File: Oauth2TokenPostHandler.java    From light-oauth2 with Apache License 2.0 4 votes vote down vote up
/**
 * This grant type is custom grant type that assume client has already authenticated the user and only send the user info
 * to the authorization server to get the access token. The token is similar with authorization code token. All extra info
 * from the formMap will be put into the token as custom claim.
 *
 * Also, only
 *
 * @param exchange
 * @param formMap
 * @return
 * @throws ApiException
 */
@SuppressWarnings("unchecked")
private Map<String, Object> handleClientAuthenticatedUser(HttpServerExchange exchange, Map<String, Object> formMap) throws ApiException {
    if(logger.isDebugEnabled()) logger.debug("client authenticated user grant formMap = " + formMap);
    Client client = authenticateClient(exchange, formMap);
    if(client != null) {
        // make sure that client is trusted
        if(Client.ClientTypeEnum.TRUSTED == client.getClientType()) {
            String scope = (String)formMap.remove("scope");
            if(scope == null) {
                scope = client.getScope(); // use the default scope defined in client if scope is not passed in
            } else {
                // make sure scope is in scope defined in client.
                if(!matchScope(scope, client.getScope())) {
                    throw new ApiException(new Status(MISMATCH_SCOPE, scope, client.getScope()));
                }
            }
            // make sure that userId and userType are passed in the formMap.
            String userId = (String)formMap.remove("userId");
            if(userId == null) {
                throw new ApiException(new Status(USER_ID_REQUIRED_FOR_CLIENT_AUTHENTICATED_USER_GRANT_TYPE));
            }

            String userType = (String)formMap.remove("userType");
            if(userType == null) {
                throw new ApiException(new Status(USER_TYPE_REQUIRED_FOR_CLIENT_AUTHENTICATED_USER_GRANT_TYPE));

            }
            String roles = (String)formMap.remove("roles");
            String jwt;
            try {
                jwt = JwtIssuer.getJwt(mockAcClaims(client.getClientId(), scope, userId, userType, roles, null, formMap));
            } catch (Exception e) {
                throw new ApiException(new Status(GENERIC_EXCEPTION, e.getMessage()));
            }

            // generate a refresh token and associate it with userId and clientId
            String refreshToken = UUID.randomUUID().toString();
            RefreshToken token = new RefreshToken();
            token.setRefreshToken(refreshToken);
            token.setUserId(userId);
            token.setUserType(userType);
            token.setRoles(roles);
            token.setClientId(client.getClientId());
            token.setScope(scope);
            token.setRemember("N"); // default to N
            IMap<String, RefreshToken> tokens = CacheStartupHookProvider.hz.getMap("tokens");
            tokens.set(refreshToken, token);

            Map<String, Object> resMap = new HashMap<>();
            resMap.put("access_token", jwt);
            resMap.put("token_type", "bearer");
            resMap.put("expires_in", config.getExpiredInMinutes()*60);
            resMap.put("refresh_token", refreshToken);
            return resMap;
        } else {
            // not trusted client, this is not allowed.
            throw new ApiException(new Status(NOT_TRUSTED_CLIENT));
        }
    }
    return new HashMap<>(); // return an empty hash map. this is actually not reachable at all.
}
 
Example 11
Source File: Oauth2UserPostHandler.java    From light-oauth2 with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
    Map<String, Object> body = (Map)exchange.getAttachment(BodyHandler.REQUEST_BODY);
    User user = Config.getInstance().getMapper().convertValue(body, User.class);

    String email = user.getEmail();
    IMap<String, User> users = CacheStartupHookProvider.hz.getMap("users");
    // make sure that email is not duplicated in users.
    Predicate predicate = new SqlPredicate(String.format("email = %s", email));
    Set<User> set = (Set<User>) users.values(predicate);
    if(set != null && set.size() > 0) {
        setExchangeStatus(exchange, EMAIL_EXISTS, email);
        processAudit(exchange);
        return;
    }

    String password = user.getPassword();
    String passwordConfirm = user.getPasswordConfirm();
    if(password != null && password.length() > 0 && passwordConfirm != null && passwordConfirm.length() > 0) {
        // check if there are the same
        if(password.equals(passwordConfirm)) {
            // hash the password with salt.
            String hashedPass = HashUtil.generateStrongPasswordHash(password);
            user.setPassword(hashedPass);
            user.setPasswordConfirm(null);
            String userId = user.getUserId();
            if(users.get(userId) == null) {
                users.set(userId, user);
            } else {
                setExchangeStatus(exchange, USER_ID_EXISTS, userId);
            }
        } else {
            // password and passwordConfirm not match.
            setExchangeStatus(exchange, PASSWORD_PASSWORDCONFIRM_NOT_MATCH, password, passwordConfirm);
        }
    } else {
        // error password or passwordConform is empty
        setExchangeStatus(exchange, PASSWORD_OR_PASSWORDCONFIRM_EMPTY, password, passwordConfirm);
    }
    processAudit(exchange);
}