lib/app/auth.go

40 lines
1.1 KiB
Go
Raw Permalink Normal View History

2021-01-19 01:50:45 +00:00
package app
import (
"code.tnxs.net/taxnexus/lib/api/auth/auth_client"
"code.tnxs.net/taxnexus/lib/api/auth/auth_client/user"
"code.tnxs.net/taxnexus/lib/api/auth/auth_models"
2021-01-30 17:06:39 +00:00
httptransport "github.com/go-openapi/runtime/client"
2021-01-19 01:50:45 +00:00
)
var authClient = auth_client.Default
2021-01-30 17:06:39 +00:00
var authUser *User
2021-01-19 01:50:45 +00:00
// CheckAPIUser is exported
func CheckAPIUser(token *string) (*User, error) {
sugar.Debug("app.CheckAPIUser: 📥")
2021-01-30 17:06:39 +00:00
if authUser == nil {
authUser = &User{
APIKey: GetServiceAccount("auth").APIKey,
Auth: httptransport.APIKeyAuth(
"X-API-Key",
"header",
GetServiceAccount("auth").APIKey,
),
}
}
2021-01-19 01:50:45 +00:00
params := user.NewGetUsersParams()
params.Apikey = token
2021-01-30 17:06:39 +00:00
response, err := authClient.User.GetUsers(params, authUser.Auth)
2021-01-19 01:50:45 +00:00
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
}