/
api.go
55 lines (49 loc) · 1.32 KB
/
api.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
package auth
import (
"context"
"beryju.io/gravity/pkg/roles/api/types"
"github.com/gorilla/sessions"
"github.com/swaggest/usecase"
"github.com/swaggest/usecase/status"
)
type APIConfigOutput struct {
Local bool `json:"bool"`
OIDC bool `json:"oidc"`
}
func (ap *AuthProvider) APIConfig() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *APIConfigOutput) error {
if ap.oidc != nil {
output.OIDC = true
}
output.Local = true
return nil
})
u.SetName("api.auth_config")
u.SetTitle("API Users")
u.SetTags("roles/api")
u.SetExpectedErrors(status.Internal)
return u
}
type APIMeOutput struct {
Username string `json:"username" required:"true"`
Authenticated bool `json:"authenticated" required:"true"`
}
func (ap *AuthProvider) APIMe() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *APIMeOutput) error {
session := ctx.Value(types.RequestSession).(*sessions.Session)
u, ok := session.Values[types.SessionKeyUser]
if u == nil || !ok {
output.Authenticated = false
return nil
}
user := u.(User)
output.Authenticated = true
output.Username = user.Username
return nil
})
u.SetName("api.users_me")
u.SetTitle("API Users")
u.SetTags("roles/api")
u.SetExpectedErrors(status.Internal)
return u
}