From 5620efe0d0fbe2f3ff26b3fef820e6a54a1c25e9 Mon Sep 17 00:00:00 2001 From: mpaktiti Date: Thu, 8 Sep 2016 14:02:35 +0300 Subject: [PATCH] Remove implementation from starter-seed --- .../com/auth0/example/PingController.java | 36 --------- .../main/java/com/auth0/example/Profile.java | 52 ------------- .../com/auth0/example/ProfileController.java | 78 ------------------- .../auth0/example/ProfileRepositoryStub.java | 70 ----------------- .../com/auth0/example/ProfileService.java | 56 ------------- .../example/ResourceNotFoundException.java | 23 ------ .../com/auth0/example/UsernameService.java | 42 ---------- 7 files changed, 357 deletions(-) delete mode 100644 00-Starter-Seed/src/main/java/com/auth0/example/PingController.java delete mode 100644 00-Starter-Seed/src/main/java/com/auth0/example/Profile.java delete mode 100644 00-Starter-Seed/src/main/java/com/auth0/example/ProfileController.java delete mode 100644 00-Starter-Seed/src/main/java/com/auth0/example/ProfileRepositoryStub.java delete mode 100644 00-Starter-Seed/src/main/java/com/auth0/example/ProfileService.java delete mode 100644 00-Starter-Seed/src/main/java/com/auth0/example/ResourceNotFoundException.java delete mode 100644 00-Starter-Seed/src/main/java/com/auth0/example/UsernameService.java diff --git a/00-Starter-Seed/src/main/java/com/auth0/example/PingController.java b/00-Starter-Seed/src/main/java/com/auth0/example/PingController.java deleted file mode 100644 index 745b7b8..0000000 --- a/00-Starter-Seed/src/main/java/com/auth0/example/PingController.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.auth0.example; - -import org.springframework.stereotype.Component; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.ResponseBody; - -@Controller -@Component -public class PingController { - - @RequestMapping(value = "/ping") - @ResponseBody - public String ping() { - return "All good. You DO NOT need to be authenticated to call /ping"; - } - - @RequestMapping(value = "/pong") - @ResponseBody - public String pong() { - return "All good. You DO NOT need to be authenticated to call /pong"; - } - - @RequestMapping(value = "/secured/ping") - @ResponseBody - public String securedPing() { - return "All good. You DO need to be authenticated to call /secured/ping"; - } - - @RequestMapping(value = "/api/v1/ping") - @ResponseBody - public String securedApiv1Ping() { - return "All good. You DO need to be authenticated to call /api/v1/ping"; - } - -} diff --git a/00-Starter-Seed/src/main/java/com/auth0/example/Profile.java b/00-Starter-Seed/src/main/java/com/auth0/example/Profile.java deleted file mode 100644 index f2587b6..0000000 --- a/00-Starter-Seed/src/main/java/com/auth0/example/Profile.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.auth0.example; - -import org.hibernate.validator.constraints.Email; - -import javax.validation.constraints.NotNull; -import javax.validation.constraints.Size; - -public class Profile { - - private Long id; - - @NotNull(message = "Name is required") - @Size(min = 3, max = 15) - private String name; - - @NotNull(message = "Email is required") - @Email(message = "Must be valid email") - private String email; - - public Profile() {} - - public Profile(final Long id, final String name, final String email) { - this.id = id; - this.name = name; - this.email = email; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getEmail() { - return email; - } - - public void setEmail(String email) { - this.email = email; - } - -} diff --git a/00-Starter-Seed/src/main/java/com/auth0/example/ProfileController.java b/00-Starter-Seed/src/main/java/com/auth0/example/ProfileController.java deleted file mode 100644 index 49a6767..0000000 --- a/00-Starter-Seed/src/main/java/com/auth0/example/ProfileController.java +++ /dev/null @@ -1,78 +0,0 @@ -package com.auth0.example; - -import com.auth0.spring.security.api.Auth0JWTToken; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.core.GrantedAuthority; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; - -import java.security.Principal; -import java.util.List; - -@RestController -@RequestMapping("/api/v1/") -public class ProfileController { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Autowired - protected AppConfig appConfig; - - @Autowired - protected ProfileService profileService; - - @Autowired - protected UsernameService usernameService; - - @RequestMapping(value = "profiles", method = RequestMethod.GET) - public List list() { - return profileService.list(); - } - - /** - * Simple demonstration of how Principal can be injected - * Here, as demonstration, we want to do audit as only ROLE_ADMIN can create user.. - */ - @RequestMapping(value ="profiles", method = RequestMethod.POST) - public Profile create(final @Validated @RequestBody Profile profile, final Principal principal) { - logger.info("create invoked"); - printGrantedAuthorities((Auth0JWTToken) principal); - if ("ROLES".equals(appConfig.getAuthorityStrategy())) { - final String username = usernameService.getUsername(); - // log username of user requesting profile creation - logger.info("User with email: " + username + " creating new profile"); - } - return profileService.create(profile); - } - - @RequestMapping(value ="profiles/{id}", method = RequestMethod.GET) - public Profile get(final @PathVariable Long id) { - logger.info("get invoked"); - return profileService.get(id); - } - - @RequestMapping(value ="profiles/{id}", method = RequestMethod.PUT) - public Profile update(final @PathVariable Long id, final @Validated @RequestBody Profile profile) { - logger.info("update invoked"); - return profileService.update(id, profile); - } - - @RequestMapping(value ="profiles/{id}", method = RequestMethod.DELETE) - public Profile delete(final @PathVariable Long id) { - logger.info("delete invoked"); - return profileService.delete(id); - } - - /** - * Simple demonstration of how Principal info can be accessed - */ - private void printGrantedAuthorities(final Auth0JWTToken principal) { - for(final GrantedAuthority grantedAuthority: principal.getAuthorities()) { - final String authority = grantedAuthority.getAuthority(); - logger.info(authority); - } - } - -} diff --git a/00-Starter-Seed/src/main/java/com/auth0/example/ProfileRepositoryStub.java b/00-Starter-Seed/src/main/java/com/auth0/example/ProfileRepositoryStub.java deleted file mode 100644 index 0c494fb..0000000 --- a/00-Starter-Seed/src/main/java/com/auth0/example/ProfileRepositoryStub.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.auth0.example; - -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -/** - * Throwaway stub for demonstration only - feel free to implement a JPA repository - * using Spring Boot - required dependencies (with h2 db) already configured - * in maven pom.xml for this sample... - */ -@Component -public class ProfileRepositoryStub { - - private static Map profiles = new HashMap(); - private static Long idIndex = 3L; - - static { - final Profile a = new Profile(1L, "Bob", "bob@secure.com"); - profiles.put(1L, a); - Profile b = new Profile(2L, "Alice", "alice@secure.com"); - profiles.put(2L, b); - Profile c = new Profile(3L, "Eve", "eve@hacker.com"); - profiles.put(3L, c); - } - - public static List list() { - return new ArrayList(profiles.values()); - } - - public static Profile create(Profile profile) { - idIndex += 1; - profile.setId(idIndex); - profiles.put(idIndex, profile); - return profile; - } - - public static Profile get(Long id) { - final Profile profile = profiles.get(id); - if (profile == null) { - throw new ResourceNotFoundException("Cannot find profile with id: " + id); - } - return profile; - } - - public static Profile update(Long id, Profile profile) { - final Profile persistedProfile = profiles.get(id); - if (persistedProfile == null) { - throw new ResourceNotFoundException("Cannot find profile with id: " + id); - } - if (profile.getName() != null) { - persistedProfile.setName(profile.getName()); - } - if (profile.getEmail() != null) { - persistedProfile.setEmail(profile.getEmail()); - } - profiles.put(id, persistedProfile); - return profiles.get(id); - } - - public static Profile delete(Long id) { - if (profiles.get(id) == null) { - throw new ResourceNotFoundException("Cannot find profile with id: " + id); - } - return profiles.remove(id); - } -} diff --git a/00-Starter-Seed/src/main/java/com/auth0/example/ProfileService.java b/00-Starter-Seed/src/main/java/com/auth0/example/ProfileService.java deleted file mode 100644 index d0e1378..0000000 --- a/00-Starter-Seed/src/main/java/com/auth0/example/ProfileService.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.auth0.example; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.stereotype.Service; - -import java.util.List; - -/** - * Secured Service - */ -@Service -public class ProfileService { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - protected ProfileRepositoryStub profileRepository; - - private Auth0Client auth0Client; - - @Autowired - public ProfileService(final Auth0Client auth0Client, final ProfileRepositoryStub profileRepository) { - this.auth0Client = auth0Client; - this.profileRepository = profileRepository; - } - - @PreAuthorize("hasAuthority('ROLE_USER') or hasAuthority('ROLE_ADMIN')") - public List list() { - return profileRepository.list(); - } - - @PreAuthorize("hasAuthority('ROLE_ADMIN')") - public Profile create(Profile profile) { - return profileRepository.create(profile); - } - - @PreAuthorize("hasAuthority('ROLE_USER') or hasAuthority('ROLE_ADMIN')") - public Profile get(Long id) { - return profileRepository.get(id); - } - - @PreAuthorize("hasAuthority('ROLE_ADMIN')") - public Profile update(Long id, Profile profile) { - return profileRepository.update(id, profile); - } - - @PreAuthorize("hasAuthority('ROLE_ADMIN')") - public Profile delete(Long id) { - return profileRepository.delete(id); - } - -} - - diff --git a/00-Starter-Seed/src/main/java/com/auth0/example/ResourceNotFoundException.java b/00-Starter-Seed/src/main/java/com/auth0/example/ResourceNotFoundException.java deleted file mode 100644 index 368e27a..0000000 --- a/00-Starter-Seed/src/main/java/com/auth0/example/ResourceNotFoundException.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.auth0.example; - -import org.springframework.http.HttpStatus; -import org.springframework.web.bind.annotation.ResponseStatus; - -@ResponseStatus(value = HttpStatus.NOT_FOUND) -public class ResourceNotFoundException extends RuntimeException { - - private static final long serialVersionUID = -4495713385368912388L; - - public ResourceNotFoundException(String msg) { - super(msg); - } - - public ResourceNotFoundException(String msg, Throwable t) { - super(msg, t); - } - - public ResourceNotFoundException(Exception e) { - super(e.getMessage(), e); - } - -} diff --git a/00-Starter-Seed/src/main/java/com/auth0/example/UsernameService.java b/00-Starter-Seed/src/main/java/com/auth0/example/UsernameService.java deleted file mode 100644 index 6e45447..0000000 --- a/00-Starter-Seed/src/main/java/com/auth0/example/UsernameService.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.auth0.example; - -import com.auth0.spring.security.api.Auth0JWTToken; -import com.auth0.spring.security.api.Auth0UserDetails; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.security.access.prepost.PreAuthorize; -import org.springframework.security.core.Authentication; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Service; - -/** - * Demonstration of method level Role based authorization - * Only an authenticated and authorized User with Admin - * rights can access this resource. - * - * Also demonstrates how to retrieve the UserDetails object - * representing the Authentication's principal from within - * a service - * - */ -@Service -public class UsernameService { - - private final Logger logger = LoggerFactory.getLogger(this.getClass()); - - @Autowired - private Auth0Client auth0Client; - - @PreAuthorize("hasAuthority('ROLE_ADMIN')") - public String getUsername() { - final Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); - final Auth0UserDetails principal = (Auth0UserDetails) authentication.getPrincipal(); - logger.info("Current user accessed Admin secured resource: " + principal.getUsername()); - // we already have the username.. but for this sample lets call Auth0 service anyway.. - return auth0Client.getUsername((Auth0JWTToken) authentication); - } - -} - -