Skip to content

Commit 68b48e4

Browse files
committed
fix: for constants usage
1 parent 4f0274f commit 68b48e4

File tree

5 files changed

+27
-18
lines changed

5 files changed

+27
-18
lines changed

axm/client/auth.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ type JWTAuthConfig struct {
4141
// NewJWTAuth creates a new OAuth 2.0 JWT authentication provider
4242
func NewJWTAuth(config JWTAuthConfig) *JWTAuth {
4343
if config.Audience == "" {
44-
config.Audience = "appstoreconnect-v1"
44+
config.Audience = DefaultJWTAudience
4545
}
4646
if config.Scope == "" {
47-
config.Scope = "business.api"
47+
config.Scope = ScopeBusinessAPI
4848
}
4949

5050
return &JWTAuth{
@@ -96,7 +96,6 @@ func (j *JWTAuth) getAccessToken() (string, error) {
9696
return "", fmt.Errorf("failed to exchange for access token: %w", err)
9797
}
9898

99-
// Store the token
10099
j.accessToken = tokenResp.AccessToken
101100
j.tokenExpiry = time.Now().Add(time.Duration(tokenResp.ExpiresIn) * time.Second)
102101

@@ -111,7 +110,7 @@ func (j *JWTAuth) generateClientAssertion() (string, error) {
111110
claims := jwt.MapClaims{
112111
"iss": j.issuerID, // team_id (issuer)
113112
"sub": j.issuerID, // client_id (subject) - same as issuer for Apple
114-
"aud": "https://account.apple.com/auth/oauth2/v2/token", // OAuth 2.0 token endpoint
113+
"aud": DefaultOAuthTokenEndpoint, // OAuth 2.0 token endpoint
115114
"iat": now.Unix(),
116115
"exp": now.Add(180 * 24 * time.Hour).Unix(), // Max 180 days as per Apple docs
117116
"jti": fmt.Sprintf("%d", now.UnixNano()), // Unique identifier
@@ -161,7 +160,7 @@ func (j *JWTAuth) exchangeForAccessToken(clientAssertion string) (*TokenResponse
161160
SetHeader("Content-Type", "application/x-www-form-urlencoded").
162161
SetHeader("Host", "account.apple.com").
163162
SetResult(&tokenResp).
164-
Post("https://account.apple.com/auth/oauth2/v2/token")
163+
Post(DefaultOAuthTokenEndpoint)
165164

166165
if err != nil {
167166
return nil, fmt.Errorf("failed to make token request: %w", err)
@@ -208,4 +207,3 @@ func (a *APIKeyAuth) ApplyAuth(req *resty.Request) error {
208207
}
209208
return nil
210209
}
211-

axm/client/client.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type APIResponse[T any] struct {
4141
// NewTransport creates a new HTTP transport for Apple Business Manager API
4242
func NewTransport(config Config) (*Client, error) {
4343
if config.BaseURL == "" {
44-
config.BaseURL = "https://api-business.apple.com/v1"
44+
config.BaseURL = DefaultBaseURL
4545
}
4646
if config.Timeout == 0 {
4747
config.Timeout = 30 * time.Second
@@ -165,12 +165,12 @@ func NewTransportFromEnv() (*Client, error) {
165165
}
166166

167167
config := Config{
168-
BaseURL: "https://api-business.apple.com/v1",
168+
BaseURL: DefaultBaseURL,
169169
Auth: NewJWTAuth(JWTAuthConfig{
170170
KeyID: keyID,
171171
IssuerID: issuerID,
172172
PrivateKey: privateKey,
173-
Audience: "appstoreconnect-v1",
173+
Audience: DefaultJWTAudience,
174174
}),
175175
Timeout: 30 * time.Second,
176176
RetryCount: 3,
@@ -199,12 +199,12 @@ func NewTransportFromFile(keyID, issuerID, privateKeyPath string) (*Client, erro
199199
}
200200

201201
config := Config{
202-
BaseURL: "https://api-business.apple.com/v1",
202+
BaseURL: DefaultBaseURL,
203203
Auth: NewJWTAuth(JWTAuthConfig{
204204
KeyID: keyID,
205205
IssuerID: issuerID,
206206
PrivateKey: privateKey,
207-
Audience: "appstoreconnect-v1",
207+
Audience: DefaultJWTAudience,
208208
}),
209209
Timeout: 30 * time.Second,
210210
RetryCount: 3,

axm/client/constants.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package client
22

3+
// API Base URL
4+
const (
5+
DefaultBaseURL = "https://api-business.apple.com/v1"
6+
)
7+
38
// OAuth scope constants
49
const (
510
ScopeBusinessAPI = "business.api"
@@ -8,6 +13,10 @@ const (
813

914
// Default OAuth endpoints
1015
const (
11-
DefaultOAuthTokenURL = "https://account.apple.com/auth/oauth2/token"
12-
DefaultOAuthAudience = "https://account.apple.com/auth/oauth2/v2/token"
16+
DefaultOAuthTokenEndpoint = "https://account.apple.com/auth/oauth2/v2/token"
17+
)
18+
19+
// Default OAuth audience
20+
const (
21+
DefaultJWTAudience = "appstoreconnect-v1"
1322
)

axm/new.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ type Client struct {
2020
// NewClient creates a new Apple Business Manager client
2121
func NewClient(keyID, issuerID string, privateKey any) (*Client, error) {
2222
config := client.Config{
23-
BaseURL: "https://api-business.apple.com/v1",
23+
BaseURL: client.DefaultBaseURL,
2424
Auth: client.NewJWTAuth(client.JWTAuthConfig{
2525
KeyID: keyID,
2626
IssuerID: issuerID,
2727
PrivateKey: privateKey,
28-
Audience: "appstoreconnect-v1",
28+
Audience: client.DefaultJWTAudience,
2929
}),
3030
Timeout: 30 * time.Second,
3131
RetryCount: 3,

examples/axm/devices/GetOrganizationDevices/main.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import (
1414
func main() {
1515
fmt.Println("=== Apple Business Manager Test Example ===")
1616

17-
keyID := "44f6a58a-xxxx-4cab-xxxx-d071a3c36a42"
18-
issuerID := "BUSINESSAPI.3bb3a62b-xxxx-4802-xxxx-a69b86201c5a"
17+
keyID := "bd4bd60b-6ddf-4fee-8f85-3ed8f6dc4bd1"
18+
issuerID := "BUSINESSAPI.3bb3a62b-6f21-4802-95ad-a69b86201c5a"
1919
privateKeyPEM := `-----BEGIN EC PRIVATE KEY-----
20-
your-abm-api-key
20+
MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgSVST2uwXoc9Gc87H
21+
uqq7jYhn+PlsrtxPQebp0LeDXp2hRANCAASBtSEWU1075awq69clg4ZPNdPiAX77
22+
mdH5iVYM8fVK1mAAk1ewo3YWlhz2GEGuox04Ng5xVrpotMQXo2WQEi9C
2123
-----END EC PRIVATE KEY-----`
2224

2325
privateKey, err := client.ParsePrivateKey([]byte(privateKeyPEM))

0 commit comments

Comments
 (0)