Skip to content

Commit

Permalink
feature/google-login
Browse files Browse the repository at this point in the history
  • Loading branch information
rohit-zip committed Jun 12, 2024
1 parent 755080b commit c22e711
Show file tree
Hide file tree
Showing 26 changed files with 233 additions and 834 deletions.
5 changes: 3 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-oauth2-client</artifactId>
<groupId>com.google.auth</groupId>
<artifactId>google-auth-library-oauth2-http</artifactId>
<version>1.23.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,37 @@
* limitations under the License.
*/

package com.bloggios.auth.provider.payload.oauth2;
package com.bloggios.auth.provider.authentication;

import com.bloggios.auth.provider.oauth2.OAuth2UserInfo;
import com.bloggios.auth.provider.constants.ExceptionCodes;
import com.bloggios.auth.provider.exception.payloads.AuthenticationException;
import com.bloggios.auth.provider.properties.AuthServerProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
* Owner - Rohit Parihar
* Owner - Rohit Parihar and Bloggios
* Author - rohit
* Project - auth-provider-application
* Package - com.bloggios.auth.provider.oauth2.OAuth2UserInfo
* Created_on - 07 February-2024
* Created_at - 17 : 48
* Package - com.bloggios.auth.provider.authentication
* Created_on - June 12 - 2024
* Created_at - 15:03
*/

public class GithubOAuth2UserInfo extends OAuth2UserInfo {
@Service
@RequiredArgsConstructor
public class GoogleTokenVerifier {

public GithubOAuth2UserInfo(Map<String, Object> attributes) {
super(attributes);
}

@Override
public String getId() {
return ((Integer) attributes.get("id")).toString();
}

@Override
public String getName() {
return (String) attributes.get("name");
}

@Override
public String getEmail() {
return (String) attributes.get("email");
}
private final AuthServerProperties authServerProperties;

@Override
public String getImageUrl() {
return (String) attributes.get("avatar_url");
public void authorize(String secret) {
Map<String, AuthServerProperties.OAuthData> oAuthData = authServerProperties.getOAuth2().getOAuthData();
AuthServerProperties.OAuthData google = oAuthData.get("google");
String googleSecret = google.getApiSecret();
if (!googleSecret.equals(secret)) {
throw new AuthenticationException(ExceptionCodes.GOOGLE_SECRET_VERIFICATION_FAILED);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@
import com.bloggios.auth.provider.authentication.BloggiosAuthenticationProvider;
import com.bloggios.auth.provider.authentication.CustomUserDetailService;
import com.bloggios.auth.provider.authentication.JwtTokenValidationFilter;
import com.bloggios.auth.provider.oauth2.CustomOAuth2UserService;
import com.bloggios.auth.provider.oauth2.HttpCookieOAuth2AuthorizationRequestRepository;
import com.bloggios.auth.provider.oauth2.OAuth2AuthenticationFailureHandler;
import com.bloggios.auth.provider.oauth2.OAuth2AuthenticationSuccessHandler;
import com.bloggios.auth.provider.properties.AuthenticationProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
Expand Down Expand Up @@ -61,33 +58,13 @@
@Configuration
@EnableWebSecurity
@EnableMethodSecurity
@RequiredArgsConstructor
public class SecurityConfiguration {

private final AuthenticationProperties authenticationProperties;
private final CustomUserDetailService customUserDetailService;
private final BloggiosAuthenticationEntryPoint bloggiosAuthenticationEntryPoint;
private final JwtTokenValidationFilter jwtTokenValidationFilter;
private final CustomOAuth2UserService customOAuth2UserService;
private final OAuth2AuthenticationSuccessHandler oAuth2AuthenticationSuccessHandler;
private final OAuth2AuthenticationFailureHandler oAuth2AuthenticationFailureHandler;

public SecurityConfiguration(
AuthenticationProperties authenticationProperties,
CustomUserDetailService customUserDetailService,
BloggiosAuthenticationEntryPoint bloggiosAuthenticationEntryPoint,
JwtTokenValidationFilter jwtTokenValidationFilter,
CustomOAuth2UserService customOAuth2UserService,
OAuth2AuthenticationSuccessHandler oAuth2AuthenticationSuccessHandler,
OAuth2AuthenticationFailureHandler oAuth2AuthenticationFailureHandler
) {
this.authenticationProperties = authenticationProperties;
this.customUserDetailService = customUserDetailService;
this.bloggiosAuthenticationEntryPoint = bloggiosAuthenticationEntryPoint;
this.jwtTokenValidationFilter = jwtTokenValidationFilter;
this.customOAuth2UserService = customOAuth2UserService;
this.oAuth2AuthenticationSuccessHandler = oAuth2AuthenticationSuccessHandler;
this.oAuth2AuthenticationFailureHandler = oAuth2AuthenticationFailureHandler;
}

@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
Expand All @@ -114,20 +91,7 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
);
})
.formLogin().disable()
.httpBasic().disable()
.oauth2Login()
.authorizationEndpoint()
.baseUri("/oauth2/authorize")
.authorizationRequestRepository(cookieAuthorizationRequestRepository())
.and()
.redirectionEndpoint()
.baseUri("/oauth2/callback/*")
.and()
.userInfoEndpoint()
.userService(customOAuth2UserService)
.and()
.successHandler(oAuth2AuthenticationSuccessHandler)
.failureHandler(oAuth2AuthenticationFailureHandler);
.httpBasic().disable();
http.addFilterBefore(jwtTokenValidationFilter, UsernamePasswordAuthenticationFilter.class);
return http.build();
}
Expand All @@ -149,9 +113,4 @@ public BloggiosAuthenticationProvider bloggiosAuthenticationProvider() {
public AuthenticationManager authenticationManager() {
return authentication -> bloggiosAuthenticationProvider().authenticate(authentication);
}

@Bean
public HttpCookieOAuth2AuthorizationRequestRepository cookieAuthorizationRequestRepository() {
return new HttpCookieOAuth2AuthorizationRequestRepository();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class WebMvc {
public CorsWebFilter corsWebFilter() {
CorsConfiguration corsConfig = new CorsConfiguration();
corsConfig.setAllowedOrigins(List.of(
"http://localhost:3000",
"http://localhost:2000",
"https://bloggios.com",
"https://dev.bloggios.in",
"https://www.bloggios.com"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static class AuthenticationController {
public static final String LOGOUT = "/logout";
public static final String USER_IP = "/user-ip";
public static final String REFRESH_TOKEN_SOCIAL = "/refresh-token-social";
public static final String GOOGLE_LOGIN = "/login-google";
}

public static class UserController {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public class ExceptionCodes {
public static final String FORGET_PASSWORD_USER_ID_NOT_MATCHED_EMAIL_USER_AUTH = "IE__AUTH-1019";
public static final String USER_ID_PASSED_NULL_OR_EMPTY_FORGET_PASSWORD_ES = "IE__AUTH-2020";
public static final String UNABLE_TO_EXTRACT_USER_IP_FROM_TOKEN = "IE__AUTH-2021";
public static final String CASTING_ERROR = "IE__AUTH-2022";


public static final String USER_NOT_FOUND_WITH_EMAIL = "DE__AUTH-2001";
Expand Down Expand Up @@ -102,4 +103,6 @@ public class ExceptionCodes {
public static final String USER_NOT_PRESENT_FOR_OTP = "DE__AUTH-2046";
public static final String OTP_NOT_PRESENT_FOR_USER = "DE__AUTH-2048";
public static final String ROLES_CANNOT_CONTAINS_NULL = "DE__AUTH-2049";
public static final String GOOGLE_SECRET_VERIFICATION_FAILED = "DE__AUTH-2050";
public static final String LOGIN_PROVIDER_NOT_VALID = "DE__AUTH-2051";
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;

/**
Expand Down Expand Up @@ -153,11 +151,15 @@ public ResponseEntity<RemoteAddressResponse> remoteAddress(HttpServletRequest re
return ResponseEntity.ok(AsyncUtils.getAsyncResult(authenticationService.remoteAddress(request)));
}

@GetMapping("/matcher")
public String getMatcher(HttpServletRequest request, @RequestParam String data) {
System.err.println(request.getRequestURI());
List<String> paths = List.of("/auth-provider/auth/**", "/auth/provider/**");
boolean isPathMatched = paths.stream().anyMatch(path -> antPathMatcher.match(path, data));
return Boolean.toString(isPathMatched);
@GetMapping(EndpointConstants.AuthenticationController.GOOGLE_LOGIN)
@CrossOrigin("*")
public ResponseEntity<AuthResponse> loginGoogle(@RequestParam String token, @RequestParam String secret, HttpServletRequest httpServletRequest) {
CompletableFuture<AuthResponse> authenticate = authenticationService.loginGoogle(token, secret, httpServletRequest);
AuthResponse asyncResult = AsyncUtils.getAsyncResult(authenticate);
return ResponseEntity
.ok()
.header(HttpHeaders.SET_COOKIE, asyncResult.getCookie().toString())
.header(ServiceConstants.COOKIE_TOKEN, asyncResult.getCookieToken())
.body(asyncResult);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public class UserDocument {
)
private String userId;

@Field(
type = FieldType.Keyword,
normalizer = ServiceConstants.DEFAULT_NORMALIZER
)
private String oauthId;

@MultiField(
mainField = @Field(
type = FieldType.Text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,14 @@
@EqualsAndHashCode(callSuper = true)
public class BadRequestException extends ExceptionProvider {

private String message;

public BadRequestException(String code) {
super(code);
}

public BadRequestException(String code, String message) {
super(code);
this.message = message;
}
}
Loading

0 comments on commit c22e711

Please sign in to comment.