Documentation
¶
Index ¶
- Constants
- func AuthMiddleware(client *saasusstainless.Client) func(http.Handler) http.Handler
- func ComputeSignature(secretKey, apiKey, method, rawURL string, body []byte) string
- func FormatAuthHeader(sig, saasID, apiKey string) string
- func GetUserInfo(ctx context.Context) (*saasusstainless.UserinfoGetResponse, bool)
- func ParseAuthHeader(authHeader string) (sig, saasID, apiKey string, ok bool)
- func RefererMiddleware(defaultReferer string) ...
- func SigV1Middleware(apiKey, secretKey, saasID string) ...
- func WithReferer(ctx context.Context, referer string) context.Context
- func WithSaaSusAuth() []option.RequestOption
- func WithSaaSusAuthConfig(apiKey, secretKey, saasID string) []option.RequestOption
- func WithXSaaSusReferer(ctx context.Context, referer string) context.Context
Constants ¶
const ( // RefererContextKey はRefererヘッダーの値をコンテキストに格納するためのキー RefererContextKey contextKey = "referer" // XSaaSusRefererContextKey はX-SaaSus-Refererヘッダーの値をコンテキストに格納するためのキー XSaaSusRefererContextKey contextKey = "xSaaSusReferer" )
const DefaultReferer = "https://api.saasus.io/"
DefaultReferer はSaaSus APIのデフォルトReferer値
const SigV1Prefix = "SAASUSSIGV1"
SigV1Prefix はSigV1認証ヘッダーのプレフィックス
Variables ¶
This section is empty.
Functions ¶
func AuthMiddleware ¶
AuthMiddleware はSaaSus IDトークンを検証し、ユーザー情報をコンテキストに格納する net/http 互換のミドルウェアを返す。
Authorization: Bearer <id_token> ヘッダーからトークンを取得し、 SaaSus APIでユーザー情報を取得してコンテキストに格納する。 トークンが無効または取得に失敗した場合は 401 Unauthorized を返す。
使用例(net/http):
mux := http.NewServeMux()
client := saasusstainless.NewClient(lib.WithSaaSusAuth()...)
http.Handle("/api/", lib.AuthMiddleware(client)(mux))
使用例(Echo):
e := echo.New() e.Use(echo.WrapMiddleware(lib.AuthMiddleware(client)))
func ComputeSignature ¶
ComputeSignature はSigV1のHMAC-SHA256署名を計算する。
署名対象データ(この順序で連結):
- タイムスタンプ(UTC、YYYYMMDDHHmm形式)
- APIキー
- HTTPメソッド(大文字)
- URLのホスト+パス+クエリ部分("//"以降)
- リクエストボディ(存在する場合)
func FormatAuthHeader ¶
FormatAuthHeader はSigV1認証ヘッダー文字列を生成する。
フォーマット:
SAASUSSIGV1 Sig=<hex_signature>, SaaSID=<saas_id>, APIKey=<api_key>
func GetUserInfo ¶
func GetUserInfo(ctx context.Context) (*saasusstainless.UserinfoGetResponse, bool)
GetUserInfo はコンテキストからユーザー情報を取得する。 AuthMiddleware によって格納されたユーザー情報を返す。
使用例(net/http):
func myHandler(w http.ResponseWriter, r *http.Request) {
userInfo, ok := lib.GetUserInfo(r.Context())
if !ok {
http.Error(w, "ユーザー情報が取得できません", http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "ユーザーID: %s", userInfo.ID)
}
使用例(Echo):
func myHandler(c echo.Context) error {
userInfo, ok := lib.GetUserInfo(c.Request().Context())
if !ok {
return echo.ErrInternalServerError
}
return c.JSON(http.StatusOK, userInfo)
}
func ParseAuthHeader ¶
ParseAuthHeader はSigV1 Authorizationヘッダーをパースする。 テスト検証用に公開する。
func RefererMiddleware ¶
func RefererMiddleware(defaultReferer string) func(*http.Request, func(*http.Request) (*http.Response, error)) (*http.Response, error)
RefererMiddleware はRefererヘッダーを付与するStainlessミドルウェアを返す。 コンテキストにReferer値が設定されている場合はそれを使用し、 設定されていない場合はデフォルト値を使用する。
使用例:
client := saasusstainless.NewClient(
option.WithMiddleware(lib.RefererMiddleware("https://api.saasus.io/")),
)
func SigV1Middleware ¶
func SigV1Middleware(apiKey, secretKey, saasID string) func(*http.Request, func(*http.Request) (*http.Response, error)) (*http.Response, error)
SigV1Middleware はSigV1認証を付与するStainlessミドルウェアを返す。 option.WithMiddleware()で使用する。
使用例:
client := saasusstainless.NewClient(
option.WithAPIKey("your-api-key"),
option.WithSecretKey("your-secret-key"),
option.WithSaasID("your-saas-id"),
option.WithMiddleware(lib.SigV1Middleware("your-api-key", "your-secret-key", "your-saas-id")),
)
func WithReferer ¶
WithReferer はコンテキストにReferer値を設定する。 SigV1ミドルウェアと併用して、リクエストごとに異なるRefererを設定する場合に使用する。
func WithSaaSusAuth ¶
func WithSaaSusAuth() []option.RequestOption
WithSaaSusAuth は環境変数からSigV1認証パラメータを読み取り、 SigV1認証ミドルウェアとRefererミドルウェアを返す。
必要な環境変数:
- SAASUS_API_KEY: APIキー
- SAASUS_SECRET_KEY: シークレットキー
- SAASUS_SAAS_ID: SaaS ID
使用例:
client := saasusstainless.NewClient(
lib.WithSaaSusAuth()...,
)
func WithSaaSusAuthConfig ¶
func WithSaaSusAuthConfig(apiKey, secretKey, saasID string) []option.RequestOption
WithSaaSusAuthConfig は指定されたパラメータでSigV1認証ミドルウェアと Refererミドルウェアを設定するRequestOptionスライスを返す。
使用例:
client := saasusstainless.NewClient(
lib.WithSaaSusAuthConfig("api-key", "secret-key", "saas-id")...,
)
Types ¶
This section is empty.