Skip to content

Commit

Permalink
Final Project Base version 2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mupezzuol committed Oct 9, 2019
1 parent 05514c3 commit 8ed5156
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 14 deletions.
Binary file added .DS_Store
Binary file not shown.
35 changes: 29 additions & 6 deletions src/main/java/com/oauth2/config/auth/OAuthConfiguration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.oauth2.config.auth;

import java.util.Arrays;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
Expand All @@ -10,7 +12,13 @@
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;
import org.springframework.security.oauth2.provider.token.TokenEnhancerChain;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

import com.oauth2.config.auth.token.CustomTokenEnhancer;

@Configuration
@EnableAuthorizationServer
Expand All @@ -37,6 +45,7 @@ public class OAuthConfiguration extends AuthorizationServerConfigurerAdapter {
@Value("${jwt.refreshTokenValiditySeconds}")
private int refreshTokenValiditySeconds;//30days


public OAuthConfiguration(AuthenticationManager authenticationManager, PasswordEncoder passwordEncoder, UserDetailsService userService) {
this.authenticationManager = authenticationManager;
this.passwordEncoder = passwordEncoder;
Expand All @@ -51,22 +60,36 @@ public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
.accessTokenValiditySeconds(accessTokenValiditySeconds)
.refreshTokenValiditySeconds(refreshTokenValiditySeconds)
.authorizedGrantTypes(authorizedGrantTypes)
.scopes("read", "write")
.resourceIds("api");
.scopes("read", "write");
}

@Override
public void configure(final AuthorizationServerEndpointsConfigurer endpoints) {
endpoints
.accessTokenConverter(accessTokenConverter())
TokenEnhancerChain tokenEnhancerChain = new TokenEnhancerChain();
tokenEnhancerChain.setTokenEnhancers(Arrays.asList(tokenEnhacer(), accessTokenConverter()));

endpoints
.tokenStore(tokenStore())
.tokenEnhancer(tokenEnhancerChain)
.reuseRefreshTokens(false)
.userDetailsService(userService)
.authenticationManager(authenticationManager);
}

@Bean
JwtAccessTokenConverter accessTokenConverter() {
JwtAccessTokenConverter converter = new JwtAccessTokenConverter();
return converter;
JwtAccessTokenConverter accessTokenConverter = new JwtAccessTokenConverter();
accessTokenConverter.setSigningKey("maracuja");
return accessTokenConverter;
}

@Bean
public TokenStore tokenStore(){
return new JwtTokenStore(accessTokenConverter());
}

public TokenEnhancer tokenEnhacer() {
return new CustomTokenEnhancer();
}

}
3 changes: 1 addition & 2 deletions src/main/java/com/oauth2/config/auth/UserPrincipal.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public String getPassword() {
return user.getPassword();
}

//UUID -> User
@Override
public String getUsername() {
return user.getUuid().toString();
return user.getEmail();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.oauth2.config.auth.token;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.TokenEnhancer;

import com.oauth2.config.auth.UserPrincipal;

public class CustomTokenEnhancer implements TokenEnhancer{

@Override
public OAuth2AccessToken enhance(OAuth2AccessToken accessToken, OAuth2Authentication authentication) {

UserPrincipal userAuth = (UserPrincipal) authentication.getPrincipal();

Map<String, Object> addInfo = new HashMap<>();
addInfo.put("user_uuid", userAuth.getUser().getUuid());

((DefaultOAuth2AccessToken) accessToken).setAdditionalInformation(addInfo);
return accessToken;
}

}
6 changes: 3 additions & 3 deletions src/main/java/com/oauth2/controllers/AuthController.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.web.bind.annotation.RestController;

import com.oauth2.entities.User;
import com.oauth2.models.dto.auth.AuthUserRoleAndAuthoritiesDTO;
import com.oauth2.models.dto.auth.AuthUserAndRolesAndAuthoritiesDTO;
import com.oauth2.services.IUserService;

import io.swagger.annotations.Api;
Expand All @@ -30,14 +30,14 @@ public class AuthController {
private IUserService userService;

@GetMapping(value = "/authorities/{uuid}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<AuthUserRoleAndAuthoritiesDTO> getAuthorities(@PathVariable String uuid){
public ResponseEntity<AuthUserAndRolesAndAuthoritiesDTO> getAuthorities(@PathVariable String uuid){
try {
UUID uuid_user = UUID.fromString(uuid.toString());

User user = userService.findByUuid(uuid_user)
.orElseThrow(() -> new UsernameNotFoundException("Error -> hasPermission for UUID: " + uuid_user));

return ResponseEntity.ok(new AuthUserRoleAndAuthoritiesDTO(user));
return ResponseEntity.ok(new AuthUserAndRolesAndAuthoritiesDTO(user));
} catch (IllegalArgumentException ie) {
log.error("Error method getAuthorities in class AuthController: "+ie.getMessage());
return ResponseEntity.badRequest().build();//400
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
@AllArgsConstructor
@NoArgsConstructor
@Data
public class AuthUserRoleAndAuthoritiesDTO {
public class AuthUserAndRolesAndAuthoritiesDTO {

private String uuid;
private String name;
private String email;
private List<AuthRolesDTO> roles;

public AuthUserRoleAndAuthoritiesDTO(User user) {
public AuthUserAndRolesAndAuthoritiesDTO(User user) {
this.uuid = user.getUuid().toString();
this.name = user.getName();
this.email = user.getEmail();
this.roles = new ArrayList<>();
this.roles.addAll(user.getRoles().stream()
.map(AuthRolesDTO::new)
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/oauth2/utils/GeneratorPassword.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.oauth2.utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

public class GeneratorPassword {
public static void main(String[] args) {
BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
System.out.println(encoder.encode("secretProjectExampleOAuth2Security"));
}
}
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jwt:
clientId: ProjectExampleOAuth2Security
client-secret: secretProjectExampleOAuth2Security
accessTokenValidititySeconds: 43200
authorizedGrantTypes: password,authorization_code,refresh_token
authorizedGrantTypes: password,refresh_token
refreshTokenValiditySeconds: 2592000

server:
Expand Down

0 comments on commit 8ed5156

Please sign in to comment.