Skip to content

Commit a080f04

Browse files
authored
Merge pull request #19 from bdpiprava/improvements
Enhanced suppport for different option and updated documentation and developer experience features
2 parents 2725eaa + 0e99432 commit a080f04

34 files changed

Lines changed: 7219 additions & 248 deletions

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ build
77
docs/.jekyll-cache
88
.bundles
99
.claude
10+
coverage*.txt

.golangci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ linters:
4949
paths:
5050
- third_party$
5151
- builtin$
52-
- examples$
52+
- ^examples/
5353

5454
formatters:
5555
enable:
@@ -60,4 +60,4 @@ formatters:
6060
paths:
6161
- third_party$
6262
- builtin$
63-
- examples$
63+
- ^examples/

Makefile

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
# To run unit tests
22
ROOT_DIR := $(shell pwd)
3+
# Other config
4+
NO_COLOR=\033[0m
5+
OK_COLOR=\033[32;01m
6+
ERROR_COLOR=\033[31;01m
7+
WARN_COLOR=\033[33;01m
38

49
tests:
5-
@echo "Running tests"
6-
go test -count=1 -race ./...
10+
@echo "$(OK_COLOR)==> Running tests...$(NO_COLOR)"
11+
@go install gotest.tools/gotestsum@latest
12+
@gotestsum --format=testname -- -v -race=1 -coverprofile=coverage_unit.txt -coverpkg=./...
713

814
# To generate static files
915
generate-doc:
10-
@echo "Generating docs"
16+
@echo "$(OK_COLOR)==> Generating docs...$(NO_COLOR)"
1117
@mkdir -p $(ROOT_DIR)/build/docs
1218
go run $(ROOT_DIR)/docs/main.go -generate
1319

README.md

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,135 @@ html, err := scalargo.NewV2(
294294
)
295295
```
296296

297+
### 🔐 **Authentication Configuration**
298+
299+
Scalar-Go provides comprehensive authentication support for modern APIs, including API Keys, HTTP Basic/Bearer, and OAuth2 flows.
300+
301+
#### **Enhanced API Key Authentication**
302+
303+
```go
304+
// Simple API Key (header-based, backward compatible)
305+
scalargo.WithAuthenticationOpts(
306+
scalargo.WithAPIKey("your-api-key"),
307+
)
308+
309+
// Custom header name
310+
scalargo.WithAuthenticationOpts(
311+
scalargo.WithAPIKey("your-api-key", scalargo.WithAPIKeyName("X-API-Key")),
312+
)
313+
314+
// Query parameter-based API Key
315+
scalargo.WithAuthenticationOpts(
316+
scalargo.WithAPIKeyQuery("api_key", "your-api-key"),
317+
)
318+
319+
// Cookie-based API Key
320+
scalargo.WithAuthenticationOpts(
321+
scalargo.WithAPIKeyCookie("session_token", "your-token"),
322+
)
323+
```
324+
325+
#### **HTTP Basic & Bearer Authentication**
326+
327+
```go
328+
// HTTP Basic Auth
329+
scalargo.WithAuthenticationOpts(
330+
scalargo.WithHTTPBasicAuth("username", "password"),
331+
)
332+
333+
// HTTP Bearer Token
334+
scalargo.WithAuthenticationOpts(
335+
scalargo.WithHTTPBearerToken("your-bearer-token"),
336+
)
337+
```
338+
339+
#### **OAuth2 Authentication**
340+
341+
Full OAuth2 support with all standard flows and PKCE.
342+
343+
**Authorization Code Flow (Recommended)**
344+
```go
345+
scalargo.WithAuthenticationOpts(
346+
scalargo.WithOAuth2AuthorizationCode(
347+
"https://auth.example.com/oauth/authorize",
348+
"https://auth.example.com/oauth/token",
349+
scalargo.WithOAuth2ClientID("my-client-id"),
350+
scalargo.WithOAuth2RedirectURI("https://myapp.com/callback"),
351+
scalargo.WithOAuth2PKCE(scalargo.PKCES256), // SHA-256 PKCE (recommended)
352+
scalargo.WithOAuth2Scopes("read:api", "write:api"),
353+
),
354+
)
355+
```
356+
357+
**Client Credentials Flow**
358+
```go
359+
scalargo.WithAuthenticationOpts(
360+
scalargo.WithOAuth2ClientCredentials(
361+
"https://auth.example.com/oauth/token",
362+
scalargo.WithOAuth2ClientID("service-account"),
363+
scalargo.WithOAuth2ClientSecret("super-secret"),
364+
),
365+
)
366+
```
367+
368+
**Advanced OAuth2 Customization**
369+
```go
370+
scalargo.WithAuthenticationOpts(
371+
scalargo.WithOAuth2AuthorizationCode(
372+
"https://auth.example.com/oauth/authorize",
373+
"https://auth.example.com/oauth/token",
374+
scalargo.WithOAuth2ClientID("my-client"),
375+
scalargo.WithOAuth2CustomToken("custom_access_token"), // Custom token field name
376+
scalargo.WithOAuth2AdditionalAuthParams(map[string]string{
377+
"audience": "https://api.example.com",
378+
}),
379+
scalargo.WithOAuth2AdditionalTokenParams(map[string]string{
380+
"resource": "https://resource.example.com",
381+
}),
382+
scalargo.WithOAuth2CredentialsLocation(scalargo.OAuth2CredentialsHeader),
383+
),
384+
)
385+
```
386+
387+
#### **Multiple Security Schemes**
388+
389+
Configure multiple authentication methods for your API:
390+
391+
```go
392+
scalargo.WithAuthenticationOpts(
393+
// Define multiple security schemes
394+
scalargo.WithSecurityScheme("api_key",
395+
scalargo.APIKeyScheme("X-API-Key", scalargo.APIKeyLocationHeader, "default-key"),
396+
),
397+
scalargo.WithSecurityScheme("bearer_auth",
398+
scalargo.BearerScheme("default-token"),
399+
),
400+
scalargo.WithSecurityScheme("oauth2",
401+
scalargo.OAuth2Scheme(
402+
scalargo.OAuth2FlowAuthorizationCode,
403+
scalargo.OAuth2Config{
404+
AuthorizationURL: "https://auth.example.com/authorize",
405+
TokenURL: "https://auth.example.com/token",
406+
ClientID: "my-client",
407+
UsePKCE: scalargo.PKCES256,
408+
SelectedScopes: []string{"read:api", "write:api"},
409+
},
410+
),
411+
),
412+
// Set preferred security scheme
413+
scalargo.WithPreferredSecurityScheme("bearer_auth"),
414+
)
415+
```
416+
417+
**PKCE Modes:**
418+
- `scalargo.PKCES256` - SHA-256 PKCE (recommended for production)
419+
- `scalargo.PKCEPlain` - Plain PKCE
420+
- `scalargo.PKCEDisabled` - Disable PKCE
421+
422+
**OAuth2 Credentials Location:**
423+
- `scalargo.OAuth2CredentialsHeader` - Send credentials in Authorization header (default)
424+
- `scalargo.OAuth2CredentialsBody` - Send credentials in request body
425+
297426
## 🚀 Real-World Examples
298427

299428
### 🏢 **Enterprise API Documentation**
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
openapi: 3.0.0
2+
info:
3+
title: Malformed Test API
4+
version: 1.0.0
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
schemas:
2+
- item1
3+
- item2
4+
- item3
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
schemas: "not an object, surprise!"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Swagger Petstore
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://petstore.swagger.io/v1
9+
tags:
10+
- name: pets
11+
description: Everything about your Pets
12+
- name: store
13+
description: Access to Petstore

0 commit comments

Comments
 (0)