Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,27 @@ Input validation helpers.
### `libCallApi`
Utilities for calling external APIs and handling auth/multi-call scenarios.

Remote APIs can authenticate with OAuth2 (`client_credentials`, `refresh_token`, optional `password` grant) or fall back to BasicAuth when `grant-type` is not configured.

Example `param.yaml`:

```yaml
remoteApis:
partner-api:
domain: https://api.partner.com
name: partner-api
auth:
grant-type: client_credentials
auth-uri: https://auth.partner.com/oauth/token
client-id: partner-client
```

Secure values (existing pattern):

- `remote-api#partner-api#client-secret`
- `remote-api#partner-api#client-id`
- `remote-api#partner-api#auth-uri` (alias: `auth-url`)

### `libCrypto`
Cryptographic and security primitives.

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ require (
golang.org/x/arch v0.20.0 // indirect
golang.org/x/crypto v0.41.0 // indirect
golang.org/x/net v0.43.0 // indirect
golang.org/x/oauth2 v0.36.0 // indirect
golang.org/x/sync v0.16.0 // indirect
golang.org/x/sys v0.35.0 // indirect
golang.org/x/text v0.28.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@ golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ=
golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc=
golang.org/x/net v0.43.0 h1:lat02VYK2j4aLzMzecihNvTlJNQUq316m2Mr9rnM6YE=
golang.org/x/net v0.43.0/go.mod h1:vhO1fvI4dGsIjh73sWfUVjj3N7CA9WkKJNQm2svM6Jg=
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw=
golang.org/x/sync v0.16.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand Down
7 changes: 7 additions & 0 deletions libApplication/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,13 @@ func InitializeApp[T any](app Application[T]) *App[T] {
cache, lock := libCallApi.InitTokenCache()
api.TokenCache = cache
api.TokenCacheLock = lock
if api.AuthData.GrantType != "" {
auth, err := libCallApi.NewOAuth2AuthFromAuthData(api.AuthData, libCallApi.NewTokenHTTPClient())
if err != nil {
log.Fatal("InitializeApp: OAuth2 auth for ", id, "=>", err)
}
api.Auth = auth
}
wsParams.RemoteApis[id] = api
}

Expand Down
46 changes: 30 additions & 16 deletions libCallApi/auth.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package libCallApi

import (
"context"
"encoding/base64"
"fmt"
"sync"
"time"

"github.com/hmmftg/requestCore/libError"
"github.com/hmmftg/requestCore/status"
"github.com/hmmftg/requestCore/webFramework"
)

type Auth struct {
Expand Down Expand Up @@ -47,7 +47,8 @@ func InitTokenCache() (*TokenCache, *sync.Mutex) {
}

type AuthSystem interface {
Login(w webFramework.WebFramework) (*TokenCache, libError.Error)
Login(ctx context.Context) (*TokenCache, libError.Error)
Refresh(ctx context.Context, refreshToken string) (*TokenCache, libError.Error)
}

func (api RemoteApi) GetBasicAuthHeader() string {
Expand All @@ -73,19 +74,38 @@ func (api RemoteApi) GetAuthHeader() (string, error) {
return fmt.Sprintf("%s %s", api.TokenCache.AccessToken.Type, api.TokenCache.AccessToken.Token), nil
}

func (api *RemoteApi) handleToken(w webFramework.WebFramework) libError.Error {
func (api *RemoteApi) handleToken(ctx context.Context) libError.Error {
api.TokenCacheLock.Lock()
defer api.TokenCacheLock.Unlock()

if api.TokenCache.AccessToken != nil && !api.TokenCache.Expired() {
// another thread handles login before us
return nil
}

if api.Auth == nil {
return libError.NewWithDescription(
status.InternalServerError,
"AUTH_SYSTEM_NOT_CONFIGURED",
"auth system of api %s is not configured",
api.Name,
)
}

if api.TokenCache.RefreshToken != nil && api.TokenCache.RefreshToken.Token != "" {
tokens, err := api.Auth.Refresh(ctx, api.TokenCache.RefreshToken.Token)
if err == nil {
api.TokenCache.AccessToken = tokens.AccessToken
if tokens.RefreshToken != nil {
api.TokenCache.RefreshToken = tokens.RefreshToken
}
return nil
}
}

api.TokenCache.AccessToken = nil
api.TokenCache.RefreshToken = nil

tokens, err := api.Auth.Login(w)
tokens, err := api.Auth.Login(ctx)
if err != nil {
return err
}
Expand All @@ -94,21 +114,15 @@ func (api *RemoteApi) handleToken(w webFramework.WebFramework) libError.Error {
return nil
}

func (api *RemoteApi) Authenticate(w webFramework.WebFramework) libError.Error {
func (api *RemoteApi) Authenticate(ctx context.Context) libError.Error {
if api.TokenCacheLock == nil {
return libError.NewWithDescription(status.InternalServerError, "TOKEN_CACHE_NOT_INITIALIZED", "token cache lock of api %s is null", api.Name)
}
if api.TokenCache.AccessToken == nil {
err := api.handleToken(w)
if err != nil {
return err
}
if api.TokenCache == nil {
return libError.NewWithDescription(status.InternalServerError, "TOKEN_CACHE_NOT_INITIALIZED", "token cache of api %s is null", api.Name)
}
if api.TokenCache.Expired() {
err := api.handleToken(w)
if err != nil {
return err
}
if api.TokenCache.AccessToken == nil || api.TokenCache.Expired() {
return api.handleToken(ctx)
}
return nil
}
43 changes: 43 additions & 0 deletions libCallApi/auth_headers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package libCallApi

import (
"context"

"github.com/hmmftg/requestCore/libError"
"github.com/hmmftg/requestCore/status"
)

func (api *RemoteApi) EnsureAuthorization(ctx context.Context, headers map[string]string) libError.Error {
if headers == nil {
return libError.NewWithDescription(
status.InternalServerError,
"AUTH_HEADERS_NIL",
"headers map is nil for api %s",
api.Name,
)
}
if _, ok := headers["Authorization"]; ok {
return nil
}
if api.Auth != nil {
if err := api.Authenticate(ctx); err != nil {
return err
}
authHeader, err := api.GetAuthHeader()
if err != nil {
return libError.NewWithDescription(
status.InternalServerError,
"AUTH_HEADER_FAILED",
"failed to build auth header for api %s: %v",
api.Name,
err,
)
}
headers["Authorization"] = authHeader
return nil
}
if api.AuthData.User != "" && api.AuthData.Password != "" {
headers["Authorization"] = api.GetBasicAuthHeader()
}
return nil
}
191 changes: 191 additions & 0 deletions libCallApi/auth_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
package libCallApi_test

import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/hmmftg/requestCore/libCallApi"
"github.com/hmmftg/requestCore/libError"
"gotest.tools/v3/assert"
)

type countingAuth struct {
logins atomic.Int32
refreshes atomic.Int32
}

func (a *countingAuth) Login(ctx context.Context) (*libCallApi.TokenCache, libError.Error) {
a.logins.Add(1)
return &libCallApi.TokenCache{
AccessToken: &libCallApi.OAuth2Token{
Token: "access-token",
Type: "Bearer",
TimeTaken: time.Now(),
ValidUntil: time.Hour,
},
}, nil
}

func (a *countingAuth) Refresh(ctx context.Context, refreshToken string) (*libCallApi.TokenCache, libError.Error) {
a.refreshes.Add(1)
return &libCallApi.TokenCache{
AccessToken: &libCallApi.OAuth2Token{
Token: "refreshed-access-token",
Type: "Bearer",
TimeTaken: time.Now(),
ValidUntil: time.Hour,
},
}, nil
}

func TestAuthenticate_CacheHitAvoidsSecondLogin(t *testing.T) {
auth := &countingAuth{}
cache, lock := libCallApi.InitTokenCache()
api := &libCallApi.RemoteApi{
Name: "test-api",
Auth: auth,
TokenCache: cache,
TokenCacheLock: lock,
}

err := api.Authenticate(context.Background())
assert.NilError(t, err)
err = api.Authenticate(context.Background())
assert.NilError(t, err)
assert.Equal(t, auth.logins.Load(), int32(1))
}

func TestAuthenticate_ExpiredTokenTriggersRefresh(t *testing.T) {
auth := &countingAuth{}
cache, lock := libCallApi.InitTokenCache()
cache.AccessToken = &libCallApi.OAuth2Token{
Token: "expired-token",
Type: "Bearer",
TimeTaken: time.Now().Add(-2 * time.Hour),
ValidUntil: time.Hour,
}
cache.RefreshToken = &libCallApi.OAuth2Token{
Token: "refresh-token",
TimeTaken: time.Now(),
ValidUntil: time.Hour,
}
api := &libCallApi.RemoteApi{
Name: "test-api",
Auth: auth,
TokenCache: cache,
TokenCacheLock: lock,
}

err := api.Authenticate(context.Background())
assert.NilError(t, err)
assert.Equal(t, auth.refreshes.Load(), int32(1))
assert.Equal(t, auth.logins.Load(), int32(0))
assert.Equal(t, api.TokenCache.AccessToken.Token, "refreshed-access-token")
}

func TestAuthenticate_ConcurrentLoginOnce(t *testing.T) {
auth := &countingAuth{}
cache, lock := libCallApi.InitTokenCache()
api := &libCallApi.RemoteApi{
Name: "test-api",
Auth: auth,
TokenCache: cache,
TokenCacheLock: lock,
}

var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()
err := api.Authenticate(context.Background())
assert.NilError(t, err)
}()
}
wg.Wait()
assert.Equal(t, auth.logins.Load(), int32(1))
}

func TestEnsureAuthorization_PreservesExplicitHeader(t *testing.T) {
api := &libCallApi.RemoteApi{
Name: "test-api",
Auth: &countingAuth{},
}
headers := map[string]string{
"Authorization": "Bearer explicit-token",
}
err := api.EnsureAuthorization(context.Background(), headers)
assert.NilError(t, err)
assert.Equal(t, headers["Authorization"], "Bearer explicit-token")
}

func TestEnsureAuthorization_BasicAuthFallback(t *testing.T) {
api := &libCallApi.RemoteApi{
Name: "test-api",
AuthData: libCallApi.Auth{
User: "user",
Password: "pass",
},
}
headers := map[string]string{}
err := api.EnsureAuthorization(context.Background(), headers)
assert.NilError(t, err)
assert.Equal(t, headers["Authorization"], api.GetBasicAuthHeader())
}

func TestPrepareCall_OAuthAuthorizationHeader(t *testing.T) {
var tokenCalls atomic.Int32
tokenServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenCalls.Add(1)
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]any{
"access_token": "oauth-access-token",
"token_type": "Bearer",
"expires_in": 3600,
})
}))
defer tokenServer.Close()

apiServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Header.Get("Authorization"), "Bearer oauth-access-token")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
}))
defer apiServer.Close()

auth, err := libCallApi.NewOAuth2AuthFromAuthData(libCallApi.Auth{
GrantType: libCallApi.GrantTypeClientCredentials,
AuthURI: tokenServer.URL,
ClientID: "client-id",
ClientSecret: "client-secret",
}, tokenServer.Client())
assert.NilError(t, err)

cache, lock := libCallApi.InitTokenCache()
remoteApi := libCallApi.RemoteApi{
Name: "partner-api",
Domain: apiServer.URL,
Auth: auth,
TokenCache: cache,
TokenCacheLock: lock,
}

callData := libCallApi.CallData[map[string]string]{
Api: remoteApi,
Path: "",
Method: http.MethodGet,
Headers: map[string]string{},
BodyType: libCallApi.Empty,
Context: context.Background(),
}

req, err := libCallApi.PrepareCall(callData)
assert.NilError(t, err)
assert.Equal(t, req.Header.Get("Authorization"), "Bearer oauth-access-token")
}
Loading
Loading