aboutsummaryrefslogtreecommitdiffhomepage
path: root/backend/auth/fortee/fortee.go
blob: 7f9d81603197059902787c12714b5fa46d079832 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
}