Search in sources :

Example 1 with ClientInitialAccessModel

use of org.keycloak.models.ClientInitialAccessModel in project keycloak by keycloak.

the class AbstractClientRegistrationProvider method create.

public ClientRepresentation create(ClientRegistrationContext context) {
    ClientRepresentation client = context.getClient();
    event.event(EventType.CLIENT_REGISTER);
    RegistrationAuth registrationAuth = auth.requireCreate(context);
    try {
        RealmModel realm = session.getContext().getRealm();
        ClientModel clientModel = ClientManager.createClient(session, realm, client);
        if (client.getDefaultRoles() != null) {
            for (String name : client.getDefaultRoles()) {
                clientModel.addDefaultRole(name);
            }
        }
        if (clientModel.isServiceAccountsEnabled()) {
            new ClientManager(new RealmManager(session)).enableServiceAccount(clientModel);
        }
        if (Boolean.TRUE.equals(client.getAuthorizationServicesEnabled())) {
            RepresentationToModel.createResourceServer(clientModel, session, true);
        }
        session.clientPolicy().triggerOnEvent(new DynamicClientRegisteredContext(context, clientModel, auth.getJwt(), realm));
        ClientRegistrationPolicyManager.triggerAfterRegister(context, registrationAuth, clientModel);
        client = ModelToRepresentation.toRepresentation(clientModel, session);
        client.setSecret(clientModel.getSecret());
        String registrationAccessToken = ClientRegistrationTokenUtils.updateRegistrationAccessToken(session, clientModel, registrationAuth);
        client.setRegistrationAccessToken(registrationAccessToken);
        if (auth.isInitialAccessToken()) {
            ClientInitialAccessModel initialAccessModel = auth.getInitialAccessModel();
            session.realms().decreaseRemainingCount(realm, initialAccessModel);
        }
        client.setDirectAccessGrantsEnabled(false);
        Stream<String> defaultRolesNames = clientModel.getDefaultRolesStream();
        if (defaultRolesNames != null) {
            client.setDefaultRoles(defaultRolesNames.toArray(String[]::new));
        }
        event.client(client.getClientId()).success();
        return client;
    } catch (ModelDuplicateException e) {
        throw new ErrorResponseException(ErrorCodes.INVALID_CLIENT_METADATA, "Client Identifier in use", Response.Status.BAD_REQUEST);
    } catch (ClientPolicyException cpe) {
        throw new ErrorResponseException(cpe.getError(), cpe.getErrorDetail(), Response.Status.BAD_REQUEST);
    }
}
Also used : ClientInitialAccessModel(org.keycloak.models.ClientInitialAccessModel) RegistrationAuth(org.keycloak.services.clientregistration.policy.RegistrationAuth) DynamicClientRegisteredContext(org.keycloak.services.clientpolicy.context.DynamicClientRegisteredContext) RealmManager(org.keycloak.services.managers.RealmManager) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) OIDCClientRepresentation(org.keycloak.representations.oidc.OIDCClientRepresentation) ClientPolicyException(org.keycloak.services.clientpolicy.ClientPolicyException) RealmModel(org.keycloak.models.RealmModel) ClientModel(org.keycloak.models.ClientModel) ClientManager(org.keycloak.services.managers.ClientManager) ModelDuplicateException(org.keycloak.models.ModelDuplicateException) ErrorResponseException(org.keycloak.services.ErrorResponseException)

Example 2 with ClientInitialAccessModel

use of org.keycloak.models.ClientInitialAccessModel in project keycloak by keycloak.

the class ClientInitialAccessResource method create.

/**
 * Create a new initial access token.
 *
 * @param config
 * @return
 */
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public ClientInitialAccessPresentation create(ClientInitialAccessCreatePresentation config, @Context final HttpResponse response) {
    auth.clients().requireManage();
    int expiration = config.getExpiration() != null ? config.getExpiration() : 0;
    int count = config.getCount() != null ? config.getCount() : 1;
    ClientInitialAccessModel clientInitialAccessModel = session.realms().createClientInitialAccessModel(realm, expiration, count);
    adminEvent.operation(OperationType.CREATE).resourcePath(session.getContext().getUri(), clientInitialAccessModel.getId()).representation(config).success();
    ClientInitialAccessPresentation rep = wrap(clientInitialAccessModel);
    String token = ClientRegistrationTokenUtils.createInitialAccessToken(session, realm, clientInitialAccessModel);
    rep.setToken(token);
    response.setStatus(Response.Status.CREATED.getStatusCode());
    response.getOutputHeaders().add(HttpHeaders.LOCATION, session.getContext().getUri().getAbsolutePathBuilder().path(clientInitialAccessModel.getId()).build().toString());
    return rep;
}
Also used : ClientInitialAccessModel(org.keycloak.models.ClientInitialAccessModel) ClientInitialAccessPresentation(org.keycloak.representations.idm.ClientInitialAccessPresentation) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 3 with ClientInitialAccessModel

use of org.keycloak.models.ClientInitialAccessModel in project keycloak by keycloak.

the class MapClientInitialAccessEntity method toModel.

static ClientInitialAccessModel toModel(MapClientInitialAccessEntity entity) {
    if (entity == null)
        return null;
    ClientInitialAccessModel model = new ClientInitialAccessModel();
    model.setId(entity.getId());
    Integer timestamp = entity.getTimestamp();
    model.setTimestamp(timestamp == null ? 0 : timestamp);
    Integer expiration = entity.getExpiration();
    model.setExpiration(expiration == null ? 0 : expiration);
    Integer count = entity.getCount();
    model.setCount(count == null ? 0 : count);
    Integer remainingCount = entity.getRemainingCount();
    model.setRemainingCount(remainingCount == null ? 0 : remainingCount);
    return model;
}
Also used : ClientInitialAccessModel(org.keycloak.models.ClientInitialAccessModel)

Aggregations

ClientInitialAccessModel (org.keycloak.models.ClientInitialAccessModel)3 Consumes (javax.ws.rs.Consumes)1 POST (javax.ws.rs.POST)1 Produces (javax.ws.rs.Produces)1 ClientModel (org.keycloak.models.ClientModel)1 ModelDuplicateException (org.keycloak.models.ModelDuplicateException)1 RealmModel (org.keycloak.models.RealmModel)1 ClientInitialAccessPresentation (org.keycloak.representations.idm.ClientInitialAccessPresentation)1 ClientRepresentation (org.keycloak.representations.idm.ClientRepresentation)1 OIDCClientRepresentation (org.keycloak.representations.oidc.OIDCClientRepresentation)1 ErrorResponseException (org.keycloak.services.ErrorResponseException)1 ClientPolicyException (org.keycloak.services.clientpolicy.ClientPolicyException)1 DynamicClientRegisteredContext (org.keycloak.services.clientpolicy.context.DynamicClientRegisteredContext)1 RegistrationAuth (org.keycloak.services.clientregistration.policy.RegistrationAuth)1 ClientManager (org.keycloak.services.managers.ClientManager)1 RealmManager (org.keycloak.services.managers.RealmManager)1