From 97fdb23b7a1b75001a2ca53ea5ec76c52c57dde3 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 11 Aug 2024 13:28:13 +0900 Subject: refactor(backend): define OpenAPI spec of fortee login API --- backend/auth/fortee/fortee.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 backend/auth/fortee/fortee.go (limited to 'backend/auth/fortee/fortee.go') diff --git a/backend/auth/fortee/fortee.go b/backend/auth/fortee/fortee.go new file mode 100644 index 0000000..7f9d816 --- /dev/null +++ b/backend/auth/fortee/fortee.go @@ -0,0 +1,42 @@ +package fortee + +import ( + "context" + "errors" + "net/http" +) + +const ( + apiEndpoint = "https://fortee.jp" +) + +var ( + ErrLoginFailed = errors.New("fortee login failed") +) + +func LoginFortee(ctx context.Context, username string, password string) error { + client, err := NewClientWithResponses(apiEndpoint, WithRequestEditorFn(addAcceptHeader)) + if err != nil { + return err + } + res, err := client.PostLoginWithFormdataBodyWithResponse(ctx, PostLoginFormdataRequestBody{ + Username: username, + Password: password, + }) + if err != nil { + return err + } + if res.StatusCode() != http.StatusOK { + return ErrLoginFailed + } + if !res.JSON200.LoggedIn { + return ErrLoginFailed + } + return nil +} + +// fortee API denies requests without Accept header. +func addAcceptHeader(_ context.Context, req *http.Request) error { + req.Header.Set("Accept", "application/json") + return nil +} -- cgit v1.2.3-70-g09d2