-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoptions_auth.go
More file actions
127 lines (119 loc) · 3.59 KB
/
options_auth.go
File metadata and controls
127 lines (119 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package httpx
import (
"encoding/base64"
"github.com/imroc/req/v3"
)
// Auth sets the Authorization header using a scheme and token.
// @group Auth
// Applies to both client defaults and request-time overrides.
//
// Example: custom auth scheme
//
// // Apply to all requests
// c := httpx.New(httpx.Auth("Token", "abc123"))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // headers => #map[string]interface {} {
// // Authorization => "Token abc123" #string
// // }
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/headers", httpx.Auth("Token", "abc123"))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // headers => #map[string]interface {} {
// // Authorization => "Token abc123" #string
// // }
// // }
func Auth(scheme, token string) OptionBuilder {
return OptionBuilder{}.Auth(scheme, token)
}
func (b OptionBuilder) Auth(scheme, token string) OptionBuilder {
value := scheme + " " + token
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonHeader("Authorization", value)
},
func(r *req.Request) {
r.SetHeader("Authorization", value)
},
))
}
// Bearer sets the Authorization header with a bearer token.
// @group Auth
// Applies to both client defaults and request-time overrides.
//
// Example: bearer auth
//
// // Apply to all requests
// c := httpx.New(httpx.Bearer("token"))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // headers => #map[string]interface {} {
// // Authorization => "Bearer token" #string
// // }
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/headers", httpx.Bearer("token"))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // headers => #map[string]interface {} {
// // Authorization => "Bearer token" #string
// // }
// // }
func Bearer(token string) OptionBuilder {
return OptionBuilder{}.Bearer(token)
}
func (b OptionBuilder) Bearer(token string) OptionBuilder {
value := "Bearer " + token
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonHeader("Authorization", value)
},
func(r *req.Request) {
r.SetBearerAuthToken(token)
},
))
}
// Basic sets HTTP basic authentication headers.
// @group Auth
// Applies to both client defaults and request-time overrides.
//
// Example: basic auth
//
// // Apply to all requests
// c := httpx.New(httpx.Basic("user", "pass"))
// res, _ := httpx.Get[map[string]any](c, "https://httpbin.org/headers")
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // headers => #map[string]interface {} {
// // Authorization => "Basic dXNlcjpwYXNz" #string
// // }
// // }
//
// // Apply to a single request
// res, _ = httpx.Get[map[string]any](c, "https://httpbin.org/headers", httpx.Basic("user", "pass"))
// httpx.Dump(res) // dumps map[string]any
// // #map[string]interface {} {
// // headers => #map[string]interface {} {
// // Authorization => "Basic dXNlcjpwYXNz" #string
// // }
// // }
func Basic(user, pass string) OptionBuilder {
return OptionBuilder{}.Basic(user, pass)
}
func (b OptionBuilder) Basic(user, pass string) OptionBuilder {
value := "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+pass))
return b.add(bothOption(
func(c *Client) {
c.req.SetCommonHeader("Authorization", value)
},
func(r *req.Request) {
r.SetBasicAuth(user, pass)
},
))
}