Skip to content

Commit acc3c87

Browse files
committed
feat: introducing the auth package (removing bascule and jwt dependency)
- removed bascule and jwt dependency (and related code) by introducing the `auth` package - ancla will receive cred principal and partner IDs via context and not via bascule Update mock.go
1 parent 35dae74 commit acc3c87

13 files changed

+365
-2531
lines changed

auth/acquire.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package auth
5+
6+
// Acquirer acquires the credential for http request authorization headers.
7+
type Acquirer interface {
8+
// Acquire gets a credential string.
9+
Acquire() (string, error)
10+
}

auth/context.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package auth
5+
6+
import "context"
7+
8+
type PartnerIDsKey struct{}
9+
10+
type PrincipalKey struct{}
11+
12+
// SetPrincipal adds the security principal to the context given, e.g. the user name or client id.
13+
func SetPrincipal(ctx context.Context, p string) context.Context {
14+
return context.WithValue(ctx, PrincipalKey{}, p)
15+
}
16+
17+
// GetPrincipal gets the security principal from the context provided.
18+
func GetPrincipal(ctx context.Context) (p string, ok bool) {
19+
p, ok = ctx.Value(PrincipalKey{}).(string)
20+
return
21+
}
22+
23+
// SetPartnerIDs adds the list of partner IDs to the context given.
24+
func SetPartnerIDs(ctx context.Context, ids []string) context.Context {
25+
return context.WithValue(ctx, PartnerIDsKey{}, ids)
26+
}
27+
28+
// GetPartnerIDs gets the list of partner IDs from the context provided.
29+
func GetPartnerIDs(ctx context.Context) (ids []string, ok bool) {
30+
ids, ok = ctx.Value(PartnerIDsKey{}).([]string)
31+
return
32+
}

auth/mock.go

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-FileCopyrightText: 2025 Comcast Cable Communications Management, LLC
2+
// SPDX-License-Identifier: Apache-2.0
3+
package auth
4+
5+
import (
6+
"github.com/stretchr/testify/mock"
7+
)
8+
9+
type MockAquirer struct {
10+
mock.Mock
11+
}
12+
13+
func (m *MockAquirer) Acquire() (string, error) {
14+
args := m.Called()
15+
16+
return args.String(0), args.Error(1)
17+
}

chrysom/basicClient.go

+22-36
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"io"
1313
"net/http"
1414

15+
"github.com/xmidt-org/ancla/auth"
1516
"github.com/xmidt-org/ancla/model"
16-
"github.com/xmidt-org/bascule/acquire"
1717
"go.uber.org/zap"
1818
)
1919

@@ -58,24 +58,18 @@ type BasicClientConfig struct {
5858

5959
// Auth provides the mechanism to add auth headers to outgoing requests.
6060
// (Optional) If not provided, no auth headers are added.
61-
Auth Auth
61+
Auth auth.Acquirer
6262
}
6363

6464
// BasicClient is the client used to make requests to Argus.
6565
type BasicClient struct {
6666
client *http.Client
67-
auth acquire.Acquirer
67+
auth auth.Acquirer
6868
storeBaseURL string
6969
bucket string
7070
getLogger func(context.Context) *zap.Logger
7171
}
7272

73-
// Auth contains authorization data for requests to Argus.
74-
type Auth struct {
75-
JWT acquire.RemoteBearerTokenAcquirerOptions
76-
Basic string
77-
}
78-
7973
type response struct {
8074
Body []byte
8175
ArgusErrorHeader string
@@ -101,19 +95,13 @@ func NewBasicClient(config BasicClientConfig,
10195
return nil, err
10296
}
10397

104-
tokenAcquirer, err := buildTokenAcquirer(config.Auth)
105-
if err != nil {
106-
return nil, err
107-
}
108-
clientStore := &BasicClient{
98+
return &BasicClient{
10999
client: config.HTTPClient,
110-
auth: tokenAcquirer,
100+
auth: config.Auth,
111101
bucket: config.Bucket,
112102
storeBaseURL: config.Address + storeAPIPath,
113103
getLogger: getLogger,
114-
}
115-
116-
return clientStore, nil
104+
}, nil
117105
}
118106

119107
// GetItems fetches all items that belong to a given owner.
@@ -213,32 +201,39 @@ func (c *BasicClient) sendRequest(ctx context.Context, owner, method, url string
213201
if err != nil {
214202
return response{}, fmt.Errorf(errWrappedFmt, errNewRequestFailure, err.Error())
215203
}
216-
err = acquire.AddAuth(r, c.auth)
217-
if err != nil {
218-
return response{}, fmt.Errorf(errWrappedFmt, ErrAuthAcquirerFailure, err.Error())
219-
}
204+
220205
if len(owner) > 0 {
221206
r.Header.Set(ItemOwnerHeaderKey, owner)
222207
}
208+
209+
if c.auth != nil {
210+
auth, err := c.auth.Acquire()
211+
if err != nil {
212+
return response{}, errors.Join(ErrAuthAcquirerFailure, err)
213+
}
214+
215+
r.Header.Set("Authorization", auth)
216+
}
217+
223218
resp, err := c.client.Do(r)
224219
if err != nil {
225220
return response{}, fmt.Errorf(errWrappedFmt, errDoRequestFailure, err.Error())
226221
}
222+
227223
defer resp.Body.Close()
228-
var sqResp = response{
224+
225+
sqResp := response{
229226
Code: resp.StatusCode,
230227
ArgusErrorHeader: resp.Header.Get(XmidtErrorHeaderKey),
231228
}
232229
bodyBytes, err := io.ReadAll(resp.Body)
233230
if err != nil {
234231
return sqResp, fmt.Errorf(errWrappedFmt, errReadingBodyFailure, err.Error())
235232
}
233+
236234
sqResp.Body = bodyBytes
237-
return sqResp, nil
238-
}
239235

240-
func isEmpty(options acquire.RemoteBearerTokenAcquirerOptions) bool {
241-
return len(options.AuthURL) < 1 || options.Buffer == 0 || options.Timeout == 0
236+
return sqResp, nil
242237
}
243238

244239
// translateNonSuccessStatusCode returns as specific error
@@ -254,15 +249,6 @@ func translateNonSuccessStatusCode(code int) error {
254249
}
255250
}
256251

257-
func buildTokenAcquirer(auth Auth) (acquire.Acquirer, error) {
258-
if !isEmpty(auth.JWT) {
259-
return acquire.NewRemoteBearerTokenAcquirer(auth.JWT)
260-
} else if len(auth.Basic) > 0 {
261-
return acquire.NewFixedAuthAcquirer(auth.Basic)
262-
}
263-
return &acquire.DefaultAcquirer{}, nil
264-
}
265-
266252
func validateBasicConfig(config *BasicClientConfig) error {
267253
if config.Address == "" {
268254
return ErrAddressEmpty

0 commit comments

Comments
 (0)