Skip to main content

2FA Service Python SDK - 다중 채널 2단계 인증 서비스

Project description

two-factor-service

SecureKey 2FA Python SDK — 다중 채널 2단계 인증 서비스

설치

pip install two-factor-service

빠른 시작

비동기 (async)

from two_factor_service import TwoFactorClient

client = TwoFactorClient(
    api_key="your-api-key",
    base_url="https://securekey.ideacode.co.kr",
)

# SMS OTP 발송
result = await client.sms.send(SendSmsOtpOptions(
    phone="01012345678",
    purpose="login",
))

# SMS OTP 검증
verify = await client.sms.verify(VerifySmsOtpOptions(
    phone="01012345678",
    code="123456",
    purpose="login",
))

print(verify.verified)  # True

동기 (sync)

from two_factor_service import TwoFactorClientSync

client = TwoFactorClientSync(
    api_key="your-api-key",
    base_url="https://securekey.ideacode.co.kr",
)

result = client.sms.send(SendSmsOtpOptions(
    phone="01012345678",
    purpose="login",
))

지원 채널

채널 메서드 설명
SMS client.sms.send() / verify() / status() SMS OTP 발송 및 검증
Email client.email.send() / verify() / status() 이메일 OTP 발송 및 검증
TOTP client.totp.setup() / verify() / status() Google Authenticator 등 TOTP 앱
WebAuthn client.webauthn.register_start() / authenticate_start() FIDO2/Passkey 생체인증
Push client.push.send() / verify() / wait_for_approval() 푸시 알림 인증

SMS OTP

from two_factor_service import TwoFactorClient, SendSmsOtpOptions, VerifySmsOtpOptions

client = TwoFactorClient(api_key="your-api-key", base_url="https://securekey.ideacode.co.kr")

# 발송
result = await client.sms.send(SendSmsOtpOptions(
    phone="01012345678",
    purpose="login",
    external_user_id="user-123",  # 선택
    service_name="MyApp",         # 선택
))
print(result.request_id, result.expires_at)

# 검증
verify = await client.sms.verify(VerifySmsOtpOptions(
    phone="01012345678",
    code="123456",
    purpose="login",
))
print(verify.verified, verify.verified_at)

# 상태 확인
status = await client.sms.status("01012345678")
print(status.has_pending_otp)

Email OTP

from two_factor_service import SendEmailOtpOptions, VerifyEmailOtpOptions

result = await client.email.send(SendEmailOtpOptions(
    email="user@example.com",
    purpose="signup",
))

verify = await client.email.verify(VerifyEmailOtpOptions(
    email="user@example.com",
    code="123456",
    purpose="signup",
))

TOTP (시간 기반 OTP)

from two_factor_service import TotpSetupOptions, TotpVerifyOptions

# 설정 (QR 코드 생성)
setup = await client.totp.setup(TotpSetupOptions(
    user_id="user-123",
    account_name="user@example.com",
))
print(setup.secret, setup.qr_code_data_url, setup.backup_codes)

# 설정 검증
await client.totp.verify_setup(TotpVerifyOptions(user_id="user-123", code="123456"))

# 이후 로그인 시 검증
verify = await client.totp.verify(TotpVerifyOptions(user_id="user-123", code="654321"))

# 백업 코드 재생성
new_codes = await client.totp.regenerate_backup_codes("user-123")

# TOTP 해제
await client.totp.remove("user-123")

WebAuthn (Passkey)

from two_factor_service import (
    WebAuthnRegisterStartOptions, WebAuthnRegisterFinishOptions,
    WebAuthnAuthenticateStartOptions, WebAuthnAuthenticateFinishOptions,
)

# 등록 시작
options = await client.webauthn.register_start(WebAuthnRegisterStartOptions(
    user_id="user-123",
    user_name="user@example.com",
    user_display_name="홍길동",
))

# 브라우저에서 credential 생성 후
credential = await client.webauthn.register_finish(WebAuthnRegisterFinishOptions(
    user_id="user-123",
    response=credential_response,
    device_name="MacBook Pro",
))

# 인증 시작
auth_options = await client.webauthn.authenticate_start(
    WebAuthnAuthenticateStartOptions(user_id="user-123")
)

# 브라우저에서 assertion 생성 후
verify = await client.webauthn.authenticate_finish(WebAuthnAuthenticateFinishOptions(
    user_id="user-123",
    response=assertion_response,
))

# 등록된 키 목록 / 삭제
credentials = await client.webauthn.list_credentials("user-123")
await client.webauthn.delete_credential("user-123", "credential-id")

Push 인증

from two_factor_service import PushRegisterDeviceOptions, PushSendOptions

# 기기 등록
device = await client.push.register_device(PushRegisterDeviceOptions(
    user_id="user-123",
    fcm_token="firebase-token",
    device_name="iPhone 15",
    platform="ios",
))

# 푸시 인증 요청
result = await client.push.send(PushSendOptions(
    user_id="user-123",
    title="로그인 인증 요청",
    body="새로운 기기에서 로그인을 시도합니다.",
))

# 승인 대기 (폴링)
verify = await client.push.wait_for_approval(
    result.request_id,
    timeout=60,
    interval=2.0,
)
print(verify.verified, verify.status)

설정 옵션

client = TwoFactorClient(
    api_key="your-api-key",                          # 필수
    base_url="https://securekey.ideacode.co.kr",     # 기본: http://localhost:8090
    timeout=30.0,                                     # 요청 타임아웃 (초), 기본: 30.0
    retries=3,                                        # 재시도 횟수, 기본: 3
    retry_delay=1.0,                                  # 재시도 간격 (초), 기본: 1.0
)

에러 처리

from two_factor_service import TwoFactorClient, TwoFactorError, VerifySmsOtpOptions

try:
    await client.sms.verify(VerifySmsOtpOptions(
        phone="01012345678", code="wrong"
    ))
except TwoFactorError as e:
    print(e.code)         # 'INVALID_OTP'
    print(e.message)      # '유효하지 않은 인증 코드입니다'
    print(e.status_code)  # 400

요구 사항

  • Python 3.8 이상
  • 의존성: httpx (비동기 HTTP 클라이언트)

라이선스

MIT — IDEACODE

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

two_factor_service-1.0.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

two_factor_service-1.0.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file two_factor_service-1.0.0.tar.gz.

File metadata

  • Download URL: two_factor_service-1.0.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for two_factor_service-1.0.0.tar.gz
Algorithm Hash digest
SHA256 625b3b5f18cc02c77f9c269f204847f657bcdb7637731405fc566eea55b79a8d
MD5 3bf7c8827a677c7cf58062567d11d47f
BLAKE2b-256 93955179c44516fea621826550d48b158bfd140f0b754338da553cfe3503155c

See more details on using hashes here.

File details

Details for the file two_factor_service-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for two_factor_service-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d66968bea41acf4f42099d14cc4cad4cb3e10d5291786256358c854c1d7ff0d
MD5 32004631a298b23f49b370efa42c0d78
BLAKE2b-256 06e36ed52a10441798c0fa2ecb199eea41673dfc8ca20e5cb6c29fdd6adc2186

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page