2021-01-19 01:50:45 +00:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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
|
|
|
)
|
|
|
|
|
2021-01-30 19:39:44 +00:00
|
|
|
// CheckAPIUser authenticates a User with the auth service
|
|
|
|
//
|
|
|
|
// When called, ChechAPIUser assumes there is an "auth" service account in the
|
|
|
|
// app configuration file.
|
|
|
|
//
|
2021-01-19 01:50:45 +00:00
|
|
|
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
|
|
|
|
}
|