Documentation
¶
Index ¶
- Constants
- Variables
- func GetApplication(c echo.Context) (*domain.Application, error)
- func MustGetApplication(c echo.Context) *domain.Application
- func NewErrInvalidAppKey(id string) error
- func NewErrInvalidApplicationID(id string) error
- func SetApplication(c echo.Context, app *domain.Application)
- type Authenticator
Constants ¶
const QueryParamAppKey = "app_key"
QueryParamAppKey 애플리케이션 인증용 쿼리 파라미터 키
Variables ¶
var ( // ErrApplicationMissingInContext Context 내에서 필수 애플리케이션 정보를 조회할 수 없을 때 반환하는 에러입니다. ErrApplicationMissingInContext = errors.New("Context에서 애플리케이션 정보를 찾을 수 없습니다") // ErrApplicationTypeMismatch Context에 저장된 객체가 예상된 *domain.Application 타입이 아닐 때 반환하는 타입 단언(Type Assertion) 에러입니다. ErrApplicationTypeMismatch = errors.New("Context에 저장된 애플리케이션 정보의 타입이 올바르지 않습니다") )
Functions ¶
func GetApplication ¶
func GetApplication(c echo.Context) (*domain.Application, error)
GetApplication Context에서 애플리케이션 정보를 조회합니다.
func MustGetApplication ¶
func MustGetApplication(c echo.Context) *domain.Application
MustGetApplication Context에서 애플리케이션 정보를 조회합니다. 인증 미들웨어를 통과하여 애플리케이션 정보가 반드시 존재한다고 보장될 때 사용합니다. 조회에 실패하면 panic이 발생하므로 주의해서 사용해야 합니다.
func NewErrInvalidAppKey ¶
NewErrInvalidAppKey 제공된 App Key가 해당 Application ID의 인증 정보와 일치하지 않을 때 반환하는 인증 에러(401 Unauthorized)를 생성합니다.
func NewErrInvalidApplicationID ¶
NewErrInvalidApplicationID 요청된 Application ID가 시스템에 등록되어 있지 않거나 식별할 수 없을 때 반환하는 인증 에러(401 Unauthorized)를 생성합니다.
func SetApplication ¶
func SetApplication(c echo.Context, app *domain.Application)
SetApplication 인증된 애플리케이션 정보를 Context에 저장합니다.
Types ¶
type Authenticator ¶
type Authenticator struct {
// contains filtered or unexported fields
}
Authenticator 애플리케이션 인증을 담당하는 인증자입니다.
이 구조체는 다음과 같은 역할을 수행합니다:
- 설정 파일에서 등록된 애플리케이션 정보를 메모리에 로드
- Application ID와 App Key를 통한 인증 처리
- 인증 실패 시 적절한 HTTP 에러 반환
Authenticator는 API 버전(v1, v2 등)과 무관하게 모든 핸들러에서 재사용 가능하며, 애플리케이션 인증이 필요한 모든 엔드포인트에서 공통으로 사용됩니다.
보안:
- App Key는 SHA-256 해시로 저장되어 메모리 덤프 공격을 방어합니다.
- Constant-Time 비교를 사용하여 타이밍 공격을 방어합니다.
동시성 안전성:
- sync.RWMutex를 사용하여 동시성 안전을 보장합니다.
- 여러 고루틴에서 동시에 Authenticate를 호출해도 안전합니다.
- 현재는 초기화 후 읽기 전용이지만, 향후 동적 추가/삭제 기능 확장 가능합니다.
사용 예시:
authenticator := auth.NewAuthenticator(appConfig)
app, err := authenticator.Authenticate(applicationID, appKey)
if err != nil {
return err // 401 Unauthorized
}
// app 사용
func NewAuthenticator ¶
func NewAuthenticator(applicationConfigs []config.ApplicationConfig) *Authenticator
NewAuthenticator 설정 파일에서 애플리케이션 정보를 로드하여 인증자를 생성합니다.
이 함수는 설정된 모든 애플리케이션의 ID, 제목, 설명, 기본 Notifier ID를 메모리에 로드하고, App Key는 SHA-256 해시로 변환하여 별도로 저장합니다.
보안:
- App Key는 SHA-256으로 해시되어 저장됩니다.
- 원본 App Key는 메모리에 저장되지 않아 메모리 덤프 공격을 방어합니다.
func (*Authenticator) Authenticate ¶
func (a *Authenticator) Authenticate(applicationID, appKey string) (*domain.Application, error)
Authenticate 애플리케이션 ID와 App Key를 검증하여 인증을 수행합니다.
인증 과정:
- Application ID로 등록된 애플리케이션 조회
- 입력받은 App Key를 SHA-256으로 해시 변환
- 저장된 해시와 Constant-Time 비교
반환값:
- 성공: 인증된 Application 객체
- 실패: 401 Unauthorized 에러 (ID 없음 또는 Key 불일치)
보안:
- Constant-Time 비교를 사용하여 타이밍 공격을 방어합니다.
- 입력받은 App Key를 SHA-256으로 해시하여 저장된 해시와 비교합니다.
이 메서드는 동시성 안전하며, 여러 고루틴에서 동시에 호출 가능합니다.