Skip to content

Commit

Permalink
Merge pull request #14 from co-co-gong/feat#12
Browse files Browse the repository at this point in the history
feat(user): read user, update user, delete user
  • Loading branch information
Zerohertz authored Oct 22, 2024
2 parents 375008b + fe35b7d commit ac6f8a1
Show file tree
Hide file tree
Showing 18 changed files with 394 additions and 135 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ out/

### VS Code ###
.vscode/

### Credentials ###
.env
k8s/secrets.yaml

### Etc ###
.DS_Store
12 changes: 4 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,10 @@ ARG GITHUB_CLIENT_ID
ARG GITHUB_CLIENT_SECRET
ARG JWT_SECRET_KEY

ENV POSTGRES_HOST=${POSTGRES_HOST}
ENV POSTGRES_PORT=${POSTGRES_PORT}
ENV POSTGRES_USER=${POSTGRES_USER}
ENV POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
ENV POSTGRES_DB=${POSTGRES_DB}
ENV GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID}
ENV GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET}
ENV JWT_SECRET_KEY=${JWT_SECRET_KEY}
# Mock environment values for test (related: issue #15)
ENV GITHUB_CLIENT_ID="GITHUB_CLIENT_ID"
ENV GITHUB_CLIENT_SECRET="GITHUB_CLIENT_SECRET"
ENV JWT_SECRET_KEY="CUL0gl15xbD4Y4DFRGCVBkLfXCodzgwOypSL82/HuD4="

WORKDIR /server
COPY src /server/src/
Expand Down
7 changes: 4 additions & 3 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ services:
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
GITHUB_REDIRECT_URI: ${GITHUB_REDIRECT_URI}
GITHUB_CLIENT_ID: ${GITHUB_CLIENT_ID}
GITHUB_CLIENT_SECRET: ${GITHUB_CLIENT_SECRET}
JWT_SECRET_KEY: ${JWT_SECRET_KEY}
Expand All @@ -18,9 +19,9 @@ services:
postgres:
image: postgres:latest
environment:
POSTGRES_USER: co-co-gong
POSTGRES_PASSWORD: mypassword
POSTGRES_DB: main
POSTGRES_USER: ${POSTGRES_USER}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
POSTGRES_DB: ${POSTGRES_DB}
PGDATA: /var/lib/postgresql/data/pgdata
ports:
- "5432:5432"
100 changes: 100 additions & 0 deletions k8s/backend.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: backend
spec:
replicas: 1
selector:
matchLabels:
app: backend
template:
metadata:
labels:
app: backend
spec:
containers:
- name: backend
image: zerohertzkr/dev:latest
env:
- name: POSTGRES_HOST
value: "postgres"
- name: POSTGRES_PORT
valueFrom:
configMapKeyRef:
name: postgres-config
key: POSTGRES_PORT
- name: POSTGRES_USER
valueFrom:
configMapKeyRef:
name: postgres-config
key: POSTGRES_USER
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: POSTGRES_PASSWORD
- name: POSTGRES_DB
valueFrom:
configMapKeyRef:
name: postgres-config
key: POSTGRES_DB
- name: GITHUB_REDIRECT_URI
valueFrom:
secretKeyRef:
name: oauth-secret
key: GITHUB_REDIRECT_URI
- name: GITHUB_CLIENT_ID
valueFrom:
secretKeyRef:
name: oauth-secret
key: GITHUB_CLIENT_ID
- name: GITHUB_CLIENT_SECRET
valueFrom:
secretKeyRef:
name: oauth-secret
key: GITHUB_CLIENT_SECRET
- name: JWT_SECRET_KEY
valueFrom:
secretKeyRef:
name: jwt-secret
key: JWT_SECRET_KEY
command:
- "scripts/local.sh"
# command:
# - "sh"
# - "-c"
# - "tail -f /dev/null"
volumeMounts:
- mountPath: /home/zerohertz/workspace
name: backend-storage
volumes:
- name: backend-storage
hostPath:
path: /home/zerohertz/Zerohertz/co-co-gong-server
type: DirectoryOrCreate
---
apiVersion: v1
kind: Service
metadata:
name: backend
spec:
ports:
- port: 8080
selector:
app: backend
---
apiVersion: traefik.io/v1alpha1
kind: IngressRoute
metadata:
name: co-co-gong
spec:
entryPoints:
- websecure
routes:
- match: Host(`co-co-gong.zerohertz.xyz`)
kind: Rule
services:
- name: backend
port: 8080
tls:
certResolver: zerohertz-resolver
47 changes: 47 additions & 0 deletions k8s/postgresql.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: postgres
spec:
replicas: 1
selector:
matchLabels:
app: postgres
template:
metadata:
labels:
app: postgres
spec:
containers:
- name: postgres
image: postgres:latest
env:
- name: POSTGRES_DB
valueFrom:
configMapKeyRef:
name: postgres-config
key: POSTGRES_DB
- name: POSTGRES_USER
valueFrom:
configMapKeyRef:
name: postgres-config
key: POSTGRES_USER
- name: POSTGRES_PASSWORD
valueFrom:
secretKeyRef:
name: postgres-secret
key: POSTGRES_PASSWORD
- name: PGDATA
value: /var/lib/postgresql/data/pgdata
ports:
- containerPort: 5432
---
apiVersion: v1
kind: Service
metadata:
name: postgres
spec:
ports:
- port: 5432
selector:
app: postgres
12 changes: 2 additions & 10 deletions scripts/compose.sh
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
#!/bin/bash

source .env
export JWT_SECRET_KEY=$(openssl rand -base64 32)
export JWT_SECRET_KEY=$(openssl rand -base64 64)

docker compose down -v
docker rmi co-co-gong-server-server --force

if docker compose build --no-cache \
--build-arg POSTGRES_HOST=${POSTGRES_HOST} \
--build-arg POSTGRES_PORT=${POSTGRES_PORT} \
--build-arg POSTGRES_USER=${POSTGRES_USER} \
--build-arg POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \
--build-arg POSTGRES_DB=${POSTGRES_DB} \
--build-arg GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID} \
--build-arg GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET} \
--build-arg JWT_SECRET_KEY=${JWT_SECRET_KEY}; then
if docker compose build --no-cache; then
echo "Build success!"
else
echo "Build failed..."
Expand Down
11 changes: 0 additions & 11 deletions scripts/docker.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,4 @@
#!/bin/bash

source .env
export JWT_SECRET_KEY=$(openssl rand -base64 32)

docker build --no-cache \
--build-arg POSTGRES_HOST=${POSTGRES_HOST} \
--build-arg POSTGRES_PORT=${POSTGRES_PORT} \
--build-arg POSTGRES_USER=${POSTGRES_USER} \
--build-arg POSTGRES_PASSWORD=${POSTGRES_PASSWORD} \
--build-arg POSTGRES_DB=${POSTGRES_DB} \
--build-arg GITHUB_CLIENT_ID=${GITHUB_CLIENT_ID} \
--build-arg GITHUB_CLIENT_SECRET=${GITHUB_CLIENT_SECRET} \
--build-arg JWT_SECRET_KEY=${JWT_SECRET_KEY} \
-t test .
7 changes: 7 additions & 0 deletions scripts/k8s.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

kubectl delete ns co-co-gong
kubectl create ns co-co-gong
kubectl apply -n co-co-gong -f k8s

# kubectl exec -it -n co-co-gong deploy/backend -- zsh
4 changes: 4 additions & 0 deletions scripts/local.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

./gradlew build
java -jar build/libs/*SNAPSHOT.jar
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
package com.server.domain.oauth.controller;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import com.server.domain.oauth.dto.GithubDto;
import com.server.domain.oauth.dto.OAuthInfo;
Expand All @@ -8,14 +25,9 @@
import com.server.global.dto.ApiResponseDto;
import com.server.global.dto.TokenDto;
import com.server.global.jwt.JwtService;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.*;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
Expand All @@ -25,6 +37,8 @@ public class OAuthLoginController {
private final UserService userService;
private final JwtService jwtService;

@Value("${spring.security.oauth2.client.registration.github.redirect-uri}")
private String redirectUri;
@Value("${spring.security.oauth2.client.registration.github.client-id}")
private String clientId;
@Value("${spring.security.oauth2.client.registration.github.client-secret}")
Expand All @@ -33,16 +47,13 @@ public class OAuthLoginController {
// 새로 추가된 로그인 시작점
@GetMapping("/login")
public ResponseEntity<String> login() {
String githubAuthUrl = "https://github.com/login/oauth/authorize" +
"?client_id=" + clientId +
"&redirect_uri=http://localhost:8080/login/oauth2/code/github";
String githubAuthUrl = String.format("%s%s%s%s%s", "https://github.com/login/oauth/authorize?client_id=",
clientId, "&redirect_uri=", redirectUri, "/login/oauth2/code/github");
return ResponseEntity.status(HttpStatus.FOUND)
.header(HttpHeaders.LOCATION, githubAuthUrl)
.build();
}



@GetMapping("/login/oauth2/code/github")
public ResponseEntity<?> githubLogin(@RequestParam String code) {
try {
Expand All @@ -53,17 +64,15 @@ public ResponseEntity<?> githubLogin(@RequestParam String code) {
"https://github.com/login/oauth/access_token",
HttpMethod.POST,
getAccessToken(code),
OAuthInfo.class
);
OAuthInfo.class);
String githubAccessToken = response.getBody().getAccessToken();

// GitHub 사용자 정보 요청
ResponseEntity<GithubDto> userInfoResponse = restTemplate.exchange(
"https://api.github.com/user",
HttpMethod.GET,
getUserInfo(githubAccessToken),
GithubDto.class
);
GithubDto.class);

GithubDto githubDto = userInfoResponse.getBody();
log.info("Received user info. Username: " + githubDto.getUsername());
Expand Down Expand Up @@ -97,14 +106,16 @@ public ResponseEntity<?> refreshToken(@RequestBody TokenDto tokenDto) {
String refreshToken = tokenDto.getRefreshToken();

if (jwtService.validateToken(refreshToken)) {
String userName = jwtService.getUserNameFromToken(refreshToken);
User user = userService.findByUserName(userName);

if (user != null && refreshToken.equals(user.getRefreshToken())) {
String newAccessToken = jwtService.createAccessToken(userName);
TokenDto newTokenDto = new TokenDto(newAccessToken, refreshToken);
return ResponseEntity.ok(ApiResponseDto.success(newTokenDto));
String userName = jwtService.extractUserName(refreshToken).get();
Optional<User> user = userService.findByUserName(userName);
if (user.isPresent()) {
if (refreshToken.equals(user.get().getRefreshToken())) {
String newAccessToken = jwtService.createAccessToken(userName);
TokenDto newTokenDto = new TokenDto(newAccessToken, refreshToken);
return ResponseEntity.ok(ApiResponseDto.success(newTokenDto));
}
}

}

return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
Expand All @@ -128,4 +139,4 @@ private HttpEntity<MultiValueMap<String, String>> getUserInfo(String accessToken
headers.setBearerAuth(accessToken);
return new HttpEntity<>(headers);
}
}
}
Loading

0 comments on commit ac6f8a1

Please sign in to comment.