Package org.mitre.oauth2.service.impl

Examples of org.mitre.oauth2.service.impl.TestDefaultOAuth2ProviderTokenService


        ClientDetailsEntity savedClient = clientService.updateClient(oldClient, newClient);

        // possibly update the token
        OAuth2AccessTokenEntity token = fetchValidRegistrationToken(auth, savedClient);

        RegisteredClient registered = new RegisteredClient(savedClient, token.getValue(), config.getIssuer() + "resource/" + UriUtils.encodePathSegment(savedClient.getClientId(), "UTF-8"));

        // send it all out to the view
        m.addAttribute("client", registered);
        m.addAttribute("code", HttpStatus.OK); // http 200
View Full Code Here


    @Override
    public RegisteredClient load(ServerConfiguration serverConfig) throws Exception {
      RestTemplate restTemplate = new RestTemplate(httpFactory);


      RegisteredClient knownClient = registeredClientService.getByIssuer(serverConfig.getIssuer());
      if (knownClient == null) {

        // dynamically register this client
        JsonObject jsonRequest = ClientDetailsEntityJsonProcessor.serialize(template);
        String serializedClient = gson.toJson(jsonRequest);

        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));

        HttpEntity<String> entity = new HttpEntity<String>(serializedClient, headers);

        String registered = restTemplate.postForObject(serverConfig.getRegistrationEndpointUri(), entity, String.class);
        // TODO: handle HTTP errors

        RegisteredClient client = ClientDetailsEntityJsonProcessor.parseRegistered(registered);

        // save this client for later
        registeredClientService.save(serverConfig.getIssuer(), client);

        return client;
      } else {

        if (knownClient.getClientId() == null) {
       
          // load this client's information from the server
          HttpHeaders headers = new HttpHeaders();
          headers.set("Authorization", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, knownClient.getRegistrationAccessToken()));
          headers.setAccept(Lists.newArrayList(MediaType.APPLICATION_JSON));
 
          HttpEntity<String> entity = new HttpEntity<String>(headers);
 
          String registered = restTemplate.exchange(knownClient.getRegistrationClientUri(), HttpMethod.GET, entity, String.class).getBody();
          // TODO: handle HTTP errors
 
          RegisteredClient client = ClientDetailsEntityJsonProcessor.parseRegistered(registered);
 
          return client;
        } else {
          // it's got a client ID from the store, don't bother trying to load it
          return knownClient;
View Full Code Here

     * @throws IOException
     */
    private void readSystemScopes(JsonReader reader) throws IOException {
        reader.beginArray();
        while (reader.hasNext()) {
            SystemScope scope = new SystemScope();
            reader.beginObject();
            while (reader.hasNext()) {
                switch (reader.peek()) {
                    case END_OBJECT:
                        continue;
                    case NAME:
                        String name = reader.nextName();
                        if (reader.peek() == JsonToken.NULL) {
                            reader.skipValue();
                        } else if (name.equals("value")) {
                            scope.setValue(reader.nextString());
                        } else if (name.equals("description")) {
                            scope.setDescription(reader.nextString());
                        } else if (name.equals("allowDynReg")) {
                            scope.setAllowDynReg(reader.nextBoolean());
                        } else if (name.equals("defaultScope")) {
                            scope.setDefaultScope(reader.nextBoolean());
                        } else if (name.equals("icon")) {
                            scope.setIcon(reader.nextString());
                        } else {
                            logger.debug("found unexpected entry");
                            reader.skipValue();
                        }
                        break;
View Full Code Here

  }

  @RequestMapping(value = "/{id}", method = RequestMethod.GET, produces = "application/json")
  public String getScope(@PathVariable("id") Long id, ModelMap m) {

    SystemScope scope = scopeService.getById(id);

    if (scope != null) {

      m.put("entity", scope);
View Full Code Here

  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @RequestMapping(value = "/{id}", method = RequestMethod.PUT, produces = "application/json", consumes = "application/json")
  public String updateScope(@PathVariable("id") Long id, @RequestBody String json, ModelMap m) {

    SystemScope existing = scopeService.getById(id);

    SystemScope scope = gson.fromJson(json, SystemScope.class);

    if (existing != null && scope != null) {

      if (existing.getId().equals(scope.getId())) {
        // sanity check

        scope = scopeService.save(scope);

        m.put("entity", scope);

        return JsonEntityView.VIEWNAME;
      } else {

        logger.error("updateScope failed; scope ids to not match: got "
            + existing.getId() + " and " + scope.getId());

        m.put("code", HttpStatus.BAD_REQUEST);
        m.put("errorMessage", "Could not update scope. Scope ids to not match: got "
            + existing.getId() + " and " + scope.getId());
        return JsonErrorView.VIEWNAME;
      }

    } else {
View Full Code Here

  }

  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @RequestMapping(value = "", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
  public String createScope(@RequestBody String json, ModelMap m) {
    SystemScope scope = gson.fromJson(json, SystemScope.class);

    SystemScope alreadyExists = scopeService.getByValue(scope.getValue());
    if (alreadyExists != null) {
      //Error, cannot save a scope with the same value as an existing one
      logger.error("Error: attempting to save a scope with a value that already exists: " + scope.getValue());
      m.put("code", HttpStatus.CONFLICT);
      m.put("errorMessage", "A scope with value " + scope.getValue() + " already exists, please choose a different value.");
View Full Code Here

  }

  @PreAuthorize("hasRole('ROLE_ADMIN')")
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public String deleteScope(@PathVariable("id") Long id, ModelMap m) {
    SystemScope existing = scopeService.getById(id);

    if (existing != null) {

      scopeService.remove(existing);
View Full Code Here

  public void prepare() {

    Mockito.reset(repository);

    // two default and dynamically registerable scopes
    defaultDynScope1 = new SystemScope(defaultDynScope1String);
    defaultDynScope2 = new SystemScope(defaultDynScope2String);
    defaultDynScope1.setAllowDynReg(true);
    defaultDynScope2.setAllowDynReg(true);
    defaultDynScope1.setDefaultScope(true);
    defaultDynScope2.setDefaultScope(true);

    // two strictly default scopes (isAllowDynReg false)
    defaultScope1 = new SystemScope(defaultScope1String);
    defaultScope2 = new SystemScope(defaultScope2String);
    defaultScope1.setDefaultScope(true);
    defaultScope2.setDefaultScope(true);

    // one strictly dynamically registerable scope (isDefault false)
    dynScope1 = new SystemScope(dynScope1String);
    dynScope1.setAllowDynReg(true);

    // extraScope1 : extra scope that is neither (defaults to false/false)
    extraScope1 = new SystemScope(extraScope1String);

    // structuredScope1 : structured scope
    structuredScope1 = new SystemScope(structuredScope1String);
    structuredScope1.setStructured(true);

    // structuredScope1Value : structured scope with value
    structuredScope1Value = new SystemScope(structuredScope1String);
    structuredScope1Value.setStructured(true);
    structuredScope1Value.setStructuredValue(structuredValue);

    allScopes = Sets.newHashSet(defaultDynScope1, defaultDynScope2, defaultScope1, defaultScope2, dynScope1, extraScope1, structuredScope1, structuredScope1Value);
    allScopeStrings = Sets.newHashSet(defaultDynScope1String, defaultDynScope2String, defaultScope1String, defaultScope2String, dynScope1String, extraScope1String, structuredScope1String, structuredScope1String + ":" + structuredValue);

    Mockito.when(repository.getByValue(defaultDynScope1String)).thenReturn(defaultDynScope1);
    Mockito.when(repository.getByValue(defaultDynScope2String)).thenReturn(defaultDynScope2);
    Mockito.when(repository.getByValue(defaultScope1String)).thenReturn(defaultScope1);
    Mockito.when(repository.getByValue(defaultScope2String)).thenReturn(defaultScope2);
    Mockito.when(repository.getByValue(dynScope1String)).thenReturn(dynScope1);
    Mockito.when(repository.getByValue(extraScope1String)).thenReturn(extraScope1);
    // we re-use this value so we've got to use thenAnswer instead
    Mockito.when(repository.getByValue(structuredScope1String)).thenAnswer(new Answer<SystemScope>() {
      @Override
      public SystemScope answer(InvocationOnMock invocation) throws Throwable {
        SystemScope s = new SystemScope(structuredScope1String);
        s.setStructured(true);
        return s;
      }

    });
View Full Code Here

    // note: we have to use "thenAnswer" here to mimic the repository not serializing the structuredValue field
    Mockito.when(repository.getByValue("foo")).thenAnswer(new Answer<SystemScope>() {
      @Override
      public SystemScope answer(InvocationOnMock invocation) throws Throwable {
        SystemScope foo = new SystemScope("foo");
        foo.setStructured(true);
        return foo;
      }

    });
View Full Code Here

     * @throws IOException
     */
    private void readSystemScopes(JsonReader reader) throws IOException {
        reader.beginArray();
        while (reader.hasNext()) {
            SystemScope scope = new SystemScope();
            reader.beginObject();
            while (reader.hasNext()) {
                switch (reader.peek()) {
                    case END_OBJECT:
                        continue;
                    case NAME:
                        String name = reader.nextName();
                        if (reader.peek() == JsonToken.NULL) {
                            reader.skipValue();
                        } else if (name.equals("value")) {
                            scope.setValue(reader.nextString());
                        } else if (name.equals("description")) {
                            scope.setDescription(reader.nextString());
                        } else if (name.equals("allowDynReg")) {
                            scope.setAllowDynReg(reader.nextBoolean());
                        } else if (name.equals("defaultScope")) {
                            scope.setDefaultScope(reader.nextBoolean());
                        } else if (name.equals("icon")) {
                            scope.setIcon(reader.nextString());
                        } else {
                            logger.debug("found unexpected entry");
                            reader.skipValue();
                        }
                        break;
View Full Code Here

TOP

Related Classes of org.mitre.oauth2.service.impl.TestDefaultOAuth2ProviderTokenService

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.