Search in sources :

Example 11 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-gateway by gravitee-io.

the class CheckSubscriptionPolicyTest method shouldContinue.

@Test
public void shouldContinue() throws PolicyException, TechnicalException {
    CheckSubscriptionPolicy policy = new CheckSubscriptionPolicy();
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    PolicyChain policyChain = mock(PolicyChain.class);
    ExecutionContext executionContext = mock(ExecutionContext.class);
    when(executionContext.getAttribute(CheckSubscriptionPolicy.CONTEXT_ATTRIBUTE_CLIENT_ID)).thenReturn("my-client-id");
    SubscriptionRepository subscriptionRepository = mock(SubscriptionRepository.class);
    when(executionContext.getComponent(SubscriptionRepository.class)).thenReturn(subscriptionRepository);
    Subscription subscription = mock(Subscription.class);
    when(subscription.getClientId()).thenReturn("my-client-id");
    when(subscriptionRepository.search(any(SubscriptionCriteria.class))).thenReturn(Collections.singletonList(subscription));
    policy.onRequest(request, response, policyChain, executionContext);
    verify(policyChain, times(1)).doNext(request, response);
}
Also used : Response(io.gravitee.gateway.api.Response) PolicyChain(io.gravitee.policy.api.PolicyChain) ExecutionContext(io.gravitee.gateway.api.ExecutionContext) SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) Request(io.gravitee.gateway.api.Request) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Example 12 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-gateway by gravitee-io.

the class CheckSubscriptionPolicy method onRequest.

@Override
protected void onRequest(Request request, Response response, PolicyChain policyChain, ExecutionContext executionContext) throws PolicyException {
    SubscriptionRepository subscriptionRepository = executionContext.getComponent(SubscriptionRepository.class);
    // Get plan and client_id from execution context
    String clientId = (String) executionContext.getAttribute(CONTEXT_ATTRIBUTE_CLIENT_ID);
    if (clientId == null || clientId.trim().isEmpty()) {
        sendError(response, policyChain, "invalid_client", "No client_id was supplied");
        return;
    }
    String plan = (String) executionContext.getAttribute(ExecutionContext.ATTR_PLAN);
    try {
        List<Subscription> subscriptions = subscriptionRepository.search(new SubscriptionCriteria.Builder().plans(Collections.singleton(plan)).clientId(clientId).status(Subscription.Status.ACCEPTED).build());
        if (subscriptions != null && !subscriptions.isEmpty()) {
            Subscription subscription = subscriptions.get(0);
            if (subscription.getClientId().equals(clientId) && (subscription.getEndingAt() == null || subscription.getEndingAt().after(Date.from(request.timestamp())))) {
                executionContext.setAttribute(ExecutionContext.ATTR_APPLICATION, subscription.getApplication());
                executionContext.setAttribute(ExecutionContext.ATTR_USER_ID, subscription.getId());
                policyChain.doNext(request, response);
                return;
            }
        }
        // As per https://tools.ietf.org/html/rfc6749#section-4.1.2.1
        sendUnauthorized(policyChain, OAUTH2_ERROR_ACCESS_DENIED);
    } catch (TechnicalException te) {
        // As per https://tools.ietf.org/html/rfc6749#section-4.1.2.1
        sendUnauthorized(policyChain, OAUTH2_ERROR_SERVER_ERROR);
    }
}
Also used : SubscriptionRepository(io.gravitee.repository.management.api.SubscriptionRepository) TechnicalException(io.gravitee.repository.exceptions.TechnicalException) SubscriptionCriteria(io.gravitee.repository.management.api.search.SubscriptionCriteria) Subscription(io.gravitee.repository.management.model.Subscription)

Example 13 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-gateway by gravitee-io.

the class SubscriptionRepositoryWrapper method search.

@Override
public List<Subscription> search(SubscriptionCriteria criteria) throws TechnicalException {
    String key = criteria.getPlans().iterator().next() + '-' + criteria.getClientId();
    Element element = this.cache.get(key);
    return (element != null) ? Collections.singletonList((Subscription) element.getObjectValue()) : null;
}
Also used : Element(net.sf.ehcache.Element) Subscription(io.gravitee.repository.management.model.Subscription)

Example 14 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-gateway by gravitee-io.

the class SubscriptionRefresher method saveOrUpdate.

private void saveOrUpdate(Subscription subscription) {
    String key = subscription.getPlan() + '-' + subscription.getClientId();
    Element element = cache.get(subscription.getId());
    if (subscription.getStatus() == Subscription.Status.CLOSED && element != null) {
        cache.removeElement(element);
        String oldKey = (String) element.getObjectValue();
        Element eltSubscription = cache.get(oldKey);
        if (eltSubscription != null && ((Subscription) eltSubscription.getObjectValue()).getId().equals(subscription.getId())) {
            cache.remove(oldKey);
        }
    } else {
        LOGGER.debug("Cache a subscription: plan[{}] application[{}] client_id[{}]", subscription.getPlan(), subscription.getApplication(), subscription.getClientId());
        cache.put(new Element(subscription.getId(), key));
        cache.put(new Element(key, subscription));
    }
}
Also used : Element(net.sf.ehcache.Element) Subscription(io.gravitee.repository.management.model.Subscription)

Example 15 with Subscription

use of io.gravitee.repository.management.model.Subscription in project gravitee-management-rest-api by gravitee-io.

the class SubscriptionServiceTest method shouldUpdateSubscriptionWithoutEndingDate.

@Test
public void shouldUpdateSubscriptionWithoutEndingDate() throws Exception {
    UpdateSubscriptionEntity updatedSubscription = new UpdateSubscriptionEntity();
    updatedSubscription.setId(SUBSCRIPTION_ID);
    // subscription object is not a mock since its state is updated by the call to subscriptionService.create()
    Subscription subscription = new Subscription();
    subscription.setId(SUBSCRIPTION_ID);
    subscription.setApplication(APPLICATION_ID);
    subscription.setPlan(PLAN_ID);
    subscription.setStatus(Subscription.Status.ACCEPTED);
    // Stub
    when(subscriptionRepository.findById(SUBSCRIPTION_ID)).thenReturn(Optional.of(subscription));
    when(subscriptionRepository.update(any())).thenAnswer(returnsFirstArg());
    when(planService.findById(PLAN_ID)).thenReturn(plan);
    when(plan.getApis()).thenReturn(Collections.singleton(API_ID));
    // Run
    subscriptionService.update(updatedSubscription);
    // Verify
    verify(subscriptionRepository, times(1)).update(any(Subscription.class));
    verify(apiKeyService, never()).findBySubscription(SUBSCRIPTION_ID);
}
Also used : Subscription(io.gravitee.repository.management.model.Subscription) Test(org.junit.Test)

Aggregations

Subscription (io.gravitee.repository.management.model.Subscription)28 Test (org.junit.Test)19 SubscriptionCriteria (io.gravitee.repository.management.api.search.SubscriptionCriteria)10 SubscriptionRepository (io.gravitee.repository.management.api.SubscriptionRepository)8 TechnicalException (io.gravitee.repository.exceptions.TechnicalException)7 ExecutionContext (io.gravitee.gateway.api.ExecutionContext)5 Request (io.gravitee.gateway.api.Request)5 Response (io.gravitee.gateway.api.Response)5 PolicyChain (io.gravitee.policy.api.PolicyChain)5 UserDetails (io.gravitee.management.idp.api.authentication.UserDetails)3 NotificationParamsBuilder (io.gravitee.management.service.notification.NotificationParamsBuilder)3 Authentication (org.springframework.security.core.Authentication)3 GrantedAuthority (org.springframework.security.core.GrantedAuthority)3 SecurityContext (org.springframework.security.core.context.SecurityContext)3 Element (net.sf.ehcache.Element)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 Page (io.gravitee.common.data.domain.Page)1 HttpHeaders (io.gravitee.common.http.HttpHeaders)1 UUID (io.gravitee.common.utils.UUID)1 io.gravitee.management.model (io.gravitee.management.model)1