Search in sources :

Example 1 with ClientStorageProviderModel

use of org.keycloak.storage.client.ClientStorageProviderModel in project keycloak by keycloak.

the class ClientStorageManager method getStorageProvider.

public static ClientStorageProvider getStorageProvider(KeycloakSession session, RealmModel realm, String componentId) {
    ComponentModel model = realm.getComponent(componentId);
    if (model == null)
        return null;
    ClientStorageProviderModel storageModel = new ClientStorageProviderModel(model);
    ClientStorageProviderFactory factory = (ClientStorageProviderFactory) session.getKeycloakSessionFactory().getProviderFactory(ClientStorageProvider.class, model.getProviderId());
    if (factory == null) {
        throw new ModelException("Could not find ClientStorageProviderFactory for: " + model.getProviderId());
    }
    return getStorageProviderInstance(session, storageModel, factory);
}
Also used : ClientStorageProvider(org.keycloak.storage.client.ClientStorageProvider) ModelException(org.keycloak.models.ModelException) ClientStorageProviderFactory(org.keycloak.storage.client.ClientStorageProviderFactory) ComponentModel(org.keycloak.component.ComponentModel) ClientStorageProviderModel(org.keycloak.storage.client.ClientStorageProviderModel)

Example 2 with ClientStorageProviderModel

use of org.keycloak.storage.client.ClientStorageProviderModel in project keycloak by keycloak.

the class OpenshiftClientStorageProviderFactory method create.

@Override
public OpenshiftClientStorageProvider create(KeycloakSession session, ComponentModel model) {
    ClientStorageProviderModel providerModel = createProviderModel(model);
    IClient client = getClient(providerModel);
    if (client != null) {
        return new OpenshiftClientStorageProvider(session, providerModel, client);
    }
    client.getAuthorizationContext().setToken(providerModel.get(OpenshiftClientStorageProviderFactory.CONFIG_PROPERTY_ACCESS_TOKEN));
    return new OpenshiftClientStorageProvider(session, providerModel, client);
}
Also used : IClient(com.openshift.restclient.IClient) ClientStorageProviderModel(org.keycloak.storage.client.ClientStorageProviderModel)

Example 3 with ClientStorageProviderModel

use of org.keycloak.storage.client.ClientStorageProviderModel in project keycloak by keycloak.

the class UserConsentModelTest method setupEnv.

public static void setupEnv(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionEnv) -> {
        KeycloakSession currentSession = sessionEnv;
        RealmManager realmManager = new RealmManager(currentSession);
        RealmModel realm = realmManager.createRealm("original");
        ClientModel fooClient = realm.addClient("foo-client");
        ClientModel barClient = realm.addClient("bar-client");
        ClientScopeModel fooScope = realm.addClientScope("foo");
        fooScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        ClientScopeModel barScope = realm.addClientScope("bar");
        fooScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        UserModel john = currentSession.users().addUser(realm, "john");
        UserModel mary = currentSession.users().addUser(realm, "mary");
        UserConsentModel johnFooGrant = new UserConsentModel(fooClient);
        johnFooGrant.addGrantedClientScope(fooScope);
        realmManager.getSession().users().addConsent(realm, john.getId(), johnFooGrant);
        UserConsentModel johnBarGrant = new UserConsentModel(barClient);
        johnBarGrant.addGrantedClientScope(barScope);
        // Update should fail as grant doesn't yet exists
        try {
            realmManager.getSession().users().updateConsent(realm, john.getId(), johnBarGrant);
            Assert.fail("Not expected to end here");
        } catch (ModelException expected) {
        }
        realmManager.getSession().users().addConsent(realm, john.getId(), johnBarGrant);
        UserConsentModel maryFooGrant = new UserConsentModel(fooClient);
        maryFooGrant.addGrantedClientScope(fooScope);
        realmManager.getSession().users().addConsent(realm, mary.getId(), maryFooGrant);
        ClientStorageProviderModel clientStorage = new ClientStorageProviderModel();
        clientStorage.setProviderId(HardcodedClientStorageProviderFactory.PROVIDER_ID);
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.CLIENT_ID, "hardcoded-client");
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.REDIRECT_URI, "http://localhost:8081/*");
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.CONSENT, "true");
        clientStorage.setParentId(realm.getId());
        clientStorageComponent = realm.addComponentModel(clientStorage);
        ClientModel hardcodedClient = currentSession.clients().getClientByClientId(realm, "hardcoded-client");
        Assert.assertNotNull(hardcodedClient);
        UserConsentModel maryHardcodedGrant = new UserConsentModel(hardcodedClient);
        realmManager.getSession().users().addConsent(realm, mary.getId(), maryHardcodedGrant);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException) KeycloakSession(org.keycloak.models.KeycloakSession) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmManager(org.keycloak.services.managers.RealmManager) ClientStorageProviderModel(org.keycloak.storage.client.ClientStorageProviderModel) UserConsentModel(org.keycloak.models.UserConsentModel)

Example 4 with ClientStorageProviderModel

use of org.keycloak.storage.client.ClientStorageProviderModel in project keycloak by keycloak.

the class UserConsentWithUserStorageModelTest method setupEnv.

public static void setupEnv(KeycloakSession session) {
    KeycloakModelUtils.runJobInTransaction(session.getKeycloakSessionFactory(), (KeycloakSession sessionSetUpEnv) -> {
        KeycloakSession currentSession = sessionSetUpEnv;
        RealmManager realmManager = new RealmManager(currentSession);
        RealmModel realm = realmManager.createRealm("original");
        UserStorageProviderModel model = new UserStorageProviderModel();
        model.setName("memory");
        model.setPriority(0);
        model.setProviderId(UserMapStorageFactory.PROVIDER_ID);
        model.setParentId(realm.getId());
        model.getConfig().putSingle(IMPORT_ENABLED, Boolean.toString(false));
        realm.addComponentModel(model);
        ClientModel fooClient = realm.addClient("foo-client");
        ClientModel barClient = realm.addClient("bar-client");
        ClientScopeModel fooScope = realm.addClientScope("foo");
        fooScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        ClientScopeModel barScope = realm.addClientScope("bar");
        fooScope.setProtocol(OIDCLoginProtocol.LOGIN_PROTOCOL);
        UserModel john = currentSession.users().addUser(realm, "john");
        UserModel mary = currentSession.users().addUser(realm, "mary");
        UserConsentModel johnFooGrant = new UserConsentModel(fooClient);
        johnFooGrant.addGrantedClientScope(fooScope);
        realmManager.getSession().users().addConsent(realm, john.getId(), johnFooGrant);
        UserConsentModel johnBarGrant = new UserConsentModel(barClient);
        johnBarGrant.addGrantedClientScope(barScope);
        // Update should fail as grant doesn't yet exists
        try {
            currentSession.users().updateConsent(realm, john.getId(), johnBarGrant);
            Assert.fail("Not expected to end here");
        } catch (ModelException expected) {
        }
        realmManager.getSession().users().addConsent(realm, john.getId(), johnBarGrant);
        UserConsentModel maryFooGrant = new UserConsentModel(fooClient);
        maryFooGrant.addGrantedClientScope(fooScope);
        realmManager.getSession().users().addConsent(realm, mary.getId(), maryFooGrant);
        ClientStorageProviderModel clientStorage = new ClientStorageProviderModel();
        clientStorage.setProviderId(HardcodedClientStorageProviderFactory.PROVIDER_ID);
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.CLIENT_ID, "hardcoded-client");
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.REDIRECT_URI, "http://localhost:8081/*");
        clientStorage.getConfig().putSingle(HardcodedClientStorageProviderFactory.CONSENT, "true");
        clientStorage.setParentId(realm.getId());
        clientStorageComponent = realm.addComponentModel(clientStorage);
        ClientModel hardcodedClient = currentSession.clients().getClientByClientId(realm, "hardcoded-client");
        Assert.assertNotNull(hardcodedClient);
        UserConsentModel maryHardcodedGrant = new UserConsentModel(hardcodedClient);
        realmManager.getSession().users().addConsent(realm, mary.getId(), maryHardcodedGrant);
    });
}
Also used : RealmModel(org.keycloak.models.RealmModel) UserModel(org.keycloak.models.UserModel) ClientModel(org.keycloak.models.ClientModel) ModelException(org.keycloak.models.ModelException) KeycloakSession(org.keycloak.models.KeycloakSession) ClientScopeModel(org.keycloak.models.ClientScopeModel) RealmManager(org.keycloak.services.managers.RealmManager) UserStorageProviderModel(org.keycloak.storage.UserStorageProviderModel) ClientStorageProviderModel(org.keycloak.storage.client.ClientStorageProviderModel) UserConsentModel(org.keycloak.models.UserConsentModel)

Example 5 with ClientStorageProviderModel

use of org.keycloak.storage.client.ClientStorageProviderModel in project keycloak by keycloak.

the class RealmCacheSession method cacheClient.

protected ClientModel cacheClient(RealmModel realm, ClientModel delegate, Long revision) {
    if (invalidations.contains(delegate.getId()))
        return delegate;
    StorageId storageId = new StorageId(delegate.getId());
    CachedClient cached = null;
    ClientAdapter adapter = null;
    if (!storageId.isLocal()) {
        ComponentModel component = realm.getComponent(storageId.getProviderId());
        ClientStorageProviderModel model = new ClientStorageProviderModel(component);
        if (!model.isEnabled()) {
            return delegate;
        }
        ClientStorageProviderModel.CachePolicy policy = model.getCachePolicy();
        if (policy != null && policy == ClientStorageProviderModel.CachePolicy.NO_CACHE) {
            return delegate;
        }
        cached = new CachedClient(revision, realm, delegate);
        adapter = new ClientAdapter(realm, cached, this);
        long lifespan = model.getLifespan();
        if (lifespan > 0) {
            cache.addRevisioned(cached, startupRevision, lifespan);
        } else {
            cache.addRevisioned(cached, startupRevision);
        }
    } else {
        cached = new CachedClient(revision, realm, delegate);
        adapter = new ClientAdapter(realm, cached, this);
        cache.addRevisioned(cached, startupRevision);
    }
    return adapter;
}
Also used : ComponentModel(org.keycloak.component.ComponentModel) ClientStorageProviderModel(org.keycloak.storage.client.ClientStorageProviderModel) StorageId(org.keycloak.storage.StorageId)

Aggregations

ClientStorageProviderModel (org.keycloak.storage.client.ClientStorageProviderModel)12 RealmModel (org.keycloak.models.RealmModel)7 Test (org.junit.Test)4 AbstractTestRealmKeycloakTest (org.keycloak.testsuite.AbstractTestRealmKeycloakTest)4 ComponentModel (org.keycloak.component.ComponentModel)3 ModelException (org.keycloak.models.ModelException)3 Calendar (java.util.Calendar)2 ClientModel (org.keycloak.models.ClientModel)2 ClientScopeModel (org.keycloak.models.ClientScopeModel)2 KeycloakSession (org.keycloak.models.KeycloakSession)2 UserConsentModel (org.keycloak.models.UserConsentModel)2 UserModel (org.keycloak.models.UserModel)2 RealmManager (org.keycloak.services.managers.RealmManager)2 StorageId (org.keycloak.storage.StorageId)2 IClient (com.openshift.restclient.IClient)1 UserStorageProviderModel (org.keycloak.storage.UserStorageProviderModel)1 ClientStorageProvider (org.keycloak.storage.client.ClientStorageProvider)1 ClientStorageProviderFactory (org.keycloak.storage.client.ClientStorageProviderFactory)1