API 레퍼런스
마지막 업데이트
github.com/pardnchiu/go-jwt/core(패키지 goJwt)의 모든 공개 함수, 메서드, 타입입니다.함수
| 시그니처 | 설명 |
|---|
func New(c Config) (*JWTAuth, error) | 기본값을 적용하고, 키를 로드하거나 생성하고, Redis에 연결해 PING한 뒤 인스턴스를 반환 |
func GetAuthDataFromGinContext(c *gin.Context) (*Auth, bool) | GinMiddleware가 저장한 사용자 데이터를 읽음 |
func GetAuthDataFromHTTPRequest(r *http.Request) (*Auth, bool) | HTTPMiddleware가 요청 컨텍스트에 저장한 사용자 데이터를 읽음 |
JWTAuth 메서드
| 시그니처 | 설명 |
|---|
func (j *JWTAuth) Create(w http.ResponseWriter, r *http.Request, auth *Auth) JWTAuthResult | Access Token과 Refresh ID를 발급하고 쿠키와 Redis에 기록. auth가 nil이면 400 data_missing 반환 |
func (j *JWTAuth) Verify(w http.ResponseWriter, r *http.Request) JWTAuthResult | 요청을 검증하고 필요하면 투명하게 갱신(검증 흐름 참고) |
func (j *JWTAuth) Revoke(w http.ResponseWriter, r *http.Request) JWTAuthResult | 쿠키를 지우고 Access Token을 폐기하며 Refresh ID를 5초 후 만료(폐기 참고) |
func (j *JWTAuth) GinMiddleware() gin.HandlerFunc | Gin 미들웨어(미들웨어 참고) |
func (j *JWTAuth) HTTPMiddleware(next http.Handler) http.Handler | net/http 미들웨어 |
func (j *JWTAuth) Close() error | Redis 연결 종료 |
JWTAuth에는 공개 필드가 없으며, 하나의 인스턴스를 여러 goroutine에서 공유할 수 있습니다.설정 타입
| 타입 | 설명 |
|---|
Config | 최상위 설정: Redis, File, Option, Cookie, CheckAuth |
Redis | Host, Port, Password, DB |
File | PrivateKeyPath, PublicKeyPath |
Option | 키 내용, 유효 기간, 쿠키 이름, MaxVersion, RefreshTTL |
Cookie | Domain, Path, SameSite, Secure, HttpOnly(모두 포인터) |
필드와 기본값은 설정을 참고하십시오.결과와 데이터 타입
- type Auth struct {
- ID string `json:"id"`
- Name string `json:"name"`
- Email string `json:"email"`
- Thumbnail string `json:"thumbnail,omitempty"`
- Scope []string `json:"scope,omitempty"`
- Role string `json:"role,omitempty"`
- Level int `json:"level,omitempty"`
- }
- type JWTAuthResult struct {
- StatusCode int `json:"status_code"`
- Success bool `json:"success"`
- Data *Auth `json:"data,omitempty"`
- Token *TokenResult `json:"token,omitempty"`
- Error string `json:"error,omitempty"`
- ErrorTag string `json:"error_tag,omitempty"`
- }
- type TokenResult struct {
- Token string `json:"token"`
- RefreshId string `json:"refresh_id"`
- }
| 필드 | 설정 시점 |
|---|
JWTAuthResult.Data | Create와 Verify 성공 시 |
JWTAuthResult.Token | Create 성공 시. Verify에서는 갱신이 일어나 성공한 경우 |
JWTAuthResult.ErrorTag | 실패 시. 값은 상태 코드와 에러 태그 참고 |
내부 상태 타입
이 타입들은 공개되어 있지만 호출자가 직접 쓸 일은 거의 없습니다:| 타입 | 설명 |
|---|
RefreshData | Redis refresh:<id>의 내용: Data, Version, Fingerprint, Exp, Iat, Jti(토큰 모델 참고) |
RefreshId | Refresh ID 해시 입력: ID, Name, Email, Fingerprint, Iat, Jti |
Pem | 파싱된 ECDSA 키 쌍. private와 public 필드는 비공개이므로 외부에서 생성하거나 읽을 수 없음 |