lib/app/auth.go

39 lines
1.1 KiB
Go
Raw Permalink Normal View History

2023-09-12 03:42:13 +00:00
package app
import (
2023-09-12 04:05:33 +00:00
"code.tnxs.net/work/lib/api/auth/auth_client/user"
"code.tnxs.net/work/lib/api/auth/auth_models"
2023-09-12 03:42:13 +00:00
httptransport "github.com/go-openapi/runtime/client"
)
// CheckAPIUser authenticates a User with the auth service
//
// When called, ChechAPIUser assumes there is an "auth" service account in the
// app configuration file.
func CheckAPIUser(token *string) (*User, error) {
sugar.Debug("app.CheckAPIUser: 📥")
if authUser == nil {
authUser = &User{
APIKey: GetServiceAccount("auth").APIKey,
Auth: httptransport.APIKeyAuth(
"X-API-Key",
"header",
GetServiceAccount("auth").APIKey,
),
}
}
params := user.NewGetUsersParams()
params.Apikey = token
response, err := authClient.User.GetUsers(params, authUser.Auth)
if err != nil {
sugar.Warnf("app.CheckAPIUser: ❗ Access attempt with invalid API key: %s", *token)
return nil, err
}
var obj *auth_models.User
for _, itm := range response.Payload.Data { // single iteration execution
obj = itm
}
sugar.Debugf("app.CheckAPIUser: 📤 👍 ID = %s, Name = %s", obj.ID, obj.Name)
return MarshalAuthUserToSwagger(obj), nil
}