feat: 구글 로그인 — 로그인 창에 구글로 계속하기 버튼, 서버가 구글 ID 토큰 서명·클라이언트 ID·만료를 확인, 처음이면 자동 닉네임으로 가입(구글 이름·이메일은 저장 안 함)

This commit is contained in:
minaChoi committed 2026-10-21 08:52:34 +09:00
1 parent eb2151164f
commit 4e9b0bacba
15 files changed
+407 -13

No files matched your search

+80
View File
@@ -0,0 +1,80 @@
import time
import pytest
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.primitives.asymmetric import rsa
from httpx import ASGITransport, AsyncClient
from jose import jwk, jwt
import common.google_id as google_id
import services.auth_service as auth_service
from config.settings import settings
from router.router import app
CLIENT_ID = "test-client.apps.googleusercontent.com"
def client():
return AsyncClient(transport=ASGITransport(app=app), base_url="http://test")
@pytest.fixture
def google_keys(monkeypatch):
"""가짜 구글 서명키. 공개키는 _keys 대신 돌려준다."""
monkeypatch.setattr(settings, "google_client_id", CLIENT_ID)
key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
pem = key.private_bytes(serialization.Encoding.PEM, serialization.PrivateFormat.PKCS8, serialization.NoEncryption())
public = jwk.construct(key.public_key().public_bytes(serialization.Encoding.PEM, serialization.PublicFormat.SubjectPublicKeyInfo), "RS256").to_dict()
public["kid"] = "k1"
async def keys(force: bool = False):
return [public]
monkeypatch.setattr(google_id, "_keys", keys)
def sign(**over):
claims = {"iss": "https://accounts.google.com", "aud": CLIENT_ID, "sub": "1234567890", "exp": int(time.time()) + 600, "iat": int(time.time()), "name": "홍길동", "email": "a@example.com"}
claims.update(over)
return jwt.encode(claims, pem, algorithm="RS256", headers={"kid": "k1"})
return sign
@pytest.mark.anyio
async def test_verify_accepts_only_our_valid_tokens(google_keys):
assert await google_id.verify(google_keys()) == "1234567890"
assert await google_id.verify(google_keys(aud="other.apps.googleusercontent.com")) is None
assert await google_id.verify(google_keys(iss="https://evil.example")) is None
assert await google_id.verify(google_keys(exp=int(time.time()) - 10)) is None
assert await google_id.verify(google_keys()[:-4] + "abcd") is None
@pytest.mark.anyio
async def test_google_login_creates_once_with_auto_nickname_and_no_real_name(google_keys):
async with client() as c:
first = (await c.post("/v1/auth/google", json={"credential": google_keys()})).json()
assert first["access_token"] and first["me"]["provider"] == 2
nick = first["me"]["nickname"]
assert "홍길동" not in nick and "@" not in nick
again = (await c.post("/v1/auth/google", json={"credential": google_keys()})).json()
assert again["me"]["nickname"] == nick and again["me"]["login_id"] == first["me"]["login_id"]
other = (await c.post("/v1/auth/google", json={"credential": google_keys(sub="999")})).json()
assert other["me"]["login_id"] != first["me"]["login_id"]
bad = (await c.post("/v1/auth/google", json={"credential": google_keys(aud="x")})).json()
assert bad["result"]["desc"] == "AUTH_FAILED"
me = (await c.get("/v1/auth/me", headers={"Authorization": f"Bearer {first['access_token']}"})).json()["me"]
assert me["nickname"] == nick
@pytest.mark.anyio
async def test_config_exposes_client_id_only_when_set(monkeypatch):
async with client() as c:
monkeypatch.setattr(settings, "google_client_id", "")
assert "google_client_id" not in (await c.get("/v1/auth/config")).json()
monkeypatch.setattr(settings, "google_client_id", CLIENT_ID)
assert (await c.get("/v1/auth/config")).json()["google_client_id"] == CLIENT_ID
def test_auto_nickname_fits_nickname_rule():
for _ in range(50):
assert auth_service.NICKNAME.match(auth_service.auto_nickname())