Skip to content

Commit

Permalink
Improvements related to connecting from react
Browse files Browse the repository at this point in the history
  • Loading branch information
naturalprogrammer committed Dec 7, 2023
1 parent 9c3792e commit 8bb7bb6
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 7 deletions.
34 changes: 34 additions & 0 deletions .github/workflows/aws-eks-deployment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: AWS EKS Deployment
run-name: ${{ github.actor }} is pushing docker image to AWS EKS

# 1
on:
push:
branches:
- main

#2
env:
DOCKER_REGISTRY_USERNAME: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}
DOCKER_REGISTRY_PASSWORD: ${{ secrets.DIGITALOCEAN_ACCESS_TOKEN }}

#3
jobs:
build_and_push_image:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Set up Java
uses: actions/setup-java@v3
with:
java-version: 21
distribution: 'temurin'
cache: 'maven'

# - name: Set App Version
# run: mvn versions:set -DnewVersion=${{ github.sha }}

- name: Build with Maven
# run: mvn --batch-mode --update-snapshots clean test
run: mvn --batch-mode --update-snapshots clean spring-boot:build-image -Dspring-boot.build-image.imageName=registry.digitalocean.com/naturalprogrammer/np-spring-mvc-demo:app
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class SecurityConfig {
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

return http
.cors(AbstractHttpConfigurer::disable)
//.cors(customizer -> customizer.configurationSource())
.csrf(AbstractHttpConfigurer::disable)
.securityContext(customizer -> customizer.securityContextRepository(new NullSecurityContextRepository()))
.sessionManagement(configurer -> configurer.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class HttpCookieOAuth2AuthorizationRequestRepository implements Authoriza
private static final int COOKIE_EXPIRY_SECONDS = 60;
public static final String AUTHORIZATION_REQUEST_COOKIE_NAME = "my_oauth2_authorization_request";
public static final String REDIRECT_URI_COOKIE_PARAM_NAME = "myRedirectUri";
public static final String CLIENT_ID_COOKIE_PARAM_NAME = "myClientId";
public static final String CLIENT_ID_COOKIE_PARAM_NAME = "myAttemptId";

/**
* Load authorization request from cookie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
record ResourceTokenExchangeRequest(

@NotBlank
String myClientId,
String myAttemptId,

Long resourceTokenValidForMillis
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ private Either<Problem, AuthTokensResource> exchangeValidated(
.map(cookie -> Either.<Problem, String>right(cookie.getValue()))
.orElseGet(() -> cookieNotFound(userId, exchangeRequest))
.filter((String cookieValue) -> cookieMatchesRequest(cookieValue, exchangeRequest, userId))
.flatMap(myClientId -> exchangeResourceToken(userId, exchangeRequest.resourceTokenValidForMillis(), request, response));
.flatMap(attemptId -> exchangeResourceToken(userId, exchangeRequest.resourceTokenValidForMillis(), request, response));
}

private Optional<Problem> cookieMatchesRequest(String cookieValue, ResourceTokenExchangeRequest exchangeRequest, UUID userId) {
if (exchangeRequest.myClientId().equals(cookieValue))
if (exchangeRequest.myAttemptId().equals(cookieValue))
return Optional.empty();
log.warn("{} cookie {} different from the given {} for user {}",
CLIENT_ID_COOKIE_PARAM_NAME, cookieValue, exchangeRequest, userId);
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/config/application-default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ logging.level:
org.apache.kafka: OFF

my:
# homepage: http://localhost:5173
# oauth2-authentication-success-url: http://localhost:5173/social-login-success?userId=%s&resourceToken=%s
homepage: http://localhost:8080
oauth2-authentication-success-url: http://localhost:8080?userId=%s&resourceToken=%s
jws:
Expand Down
10 changes: 10 additions & 0 deletions src/main/resources/config/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ spring:
use-new-id-generator-mappings: false
ddl-auto: validate

cors:
origins:
- ${my.homepage}
methods:
# - GET
- POST
- PUT
- DELETE
- OPTIONS
maxAge: 3600
security:
strategy: MODE_INHERITABLETHREADLOCAL
oauth2:
Expand Down
4 changes: 2 additions & 2 deletions src/main/resources/static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<body>
<div class="container unauthenticated">
Login With Google (v3): <a
href="/oauth2/authorization/google?myClientId=a-unique-id">click
href="/oauth2/authorization/google?myAttemptId=a-unique-id">click
here</a>
</div>
<div class="container authenticated" style="display:none">
Expand Down Expand Up @@ -51,7 +51,7 @@
'Content-Type': 'application/vnd.com.naturalprogrammer.resource-token-exchange-request.v1+json'
},
data: JSON.stringify({
'myClientId': 'a-unique-id'
'myAttemptId': 'a-unique-id'
}),
success: function (data) {
getUser(data);
Expand Down

0 comments on commit 8bb7bb6

Please sign in to comment.