@@ -4,27 +4,29 @@ import (
44 "context"
55 "encoding/json"
66 "io"
7- "io/ioutil"
87 "net/http"
98 "net/url"
109 "strings"
1110)
1211
13- // Client oauth2 client
12+ // Client OAuth2客户端 / OAuth2 client for making authorization requests
1413type Client struct {
15- Log Logger
16- httpClient * http.Client
17- ServerBaseURL string
18- AuthorizationEndpoint string
19- TokenEndpoint string
20- IntrospectEndpoint string
21- DeviceAuthorizationEndpoint string
22- TokenRevocationEndpoint string
23- ID string
24- Secret string
14+ Log Logger // 日志记录器 / Logger instance
15+ httpClient * http.Client // HTTP客户端 / HTTP client for requests
16+ ServerBaseURL string // 服务器基础URL / OAuth2 server base URL
17+ AuthorizationEndpoint string // 授权端点 / Authorization endpoint path
18+ TokenEndpoint string // 令牌端点 / Token endpoint path
19+ IntrospectEndpoint string // 内省端点 / Introspection endpoint path
20+ DeviceAuthorizationEndpoint string // 设备授权端点 / Device authorization endpoint path
21+ TokenRevocationEndpoint string // 令牌撤销端点 / Token revocation endpoint path
22+ ID string // 客户端ID / Client identifier
23+ Secret string // 客户端密钥 / Client secret
2524}
2625
27- // NewClient new oauth2 client
26+ // NewClient 创建OAuth2客户端 / Create a new OAuth2 client
27+ // serverBaseURL: 服务器基础URL / OAuth2 server base URL
28+ // id: 客户端ID / Client identifier
29+ // secret: 客户端密钥 / Client secret
2830func NewClient (serverBaseURL , id , secret string ) * Client {
2931 httpclient := & http.Client {}
3032 httpclient .CheckRedirect = func (req * http.Request , via []* http.Request ) error {
@@ -73,13 +75,18 @@ func (c *Client) authorize(ctx context.Context, w http.ResponseWriter, responseT
7375 return
7476}
7577
76- // AuthorizeAuthorizationCode ...
78+ // AuthorizeAuthorizationCode 授权码模式授权请求 / Authorization code grant authorization request
79+ // redirectURI: 重定向URI / Redirect URI after authorization
80+ // scope: 授权范围 / Requested scope
81+ // state: 状态码,用于防止CSRF攻击 / State parameter for CSRF protection
7782func (c * Client ) AuthorizeAuthorizationCode (ctx context.Context , w http.ResponseWriter , redirectURI , scope , state string ) (err error ) {
7883 return c .authorize (ctx , w , CodeKey , redirectURI , scope , state )
7984}
8085
81- // TokenAuthorizationCode ...
82- // TokenAuthorizationCode(code, redirectURI, state string)
86+ // TokenAuthorizationCode 授权码模式获取令牌 / Exchange authorization code for access token
87+ // code: 授权码 / Authorization code received from authorization server
88+ // redirectURI: 重定向URI / Redirect URI used in authorization request
89+ // clientID: 客户端ID / Client identifier
8390func (c * Client ) TokenAuthorizationCode (ctx context.Context , code , redirectURI , clientID string ) (token * TokenResponse , err error ) {
8491 values := url.Values {
8592 CodeKey : []string {code },
@@ -89,12 +96,16 @@ func (c *Client) TokenAuthorizationCode(ctx context.Context, code, redirectURI,
8996 return c .token (ctx , AuthorizationCodeKey , values )
9097}
9198
92- // AuthorizeImplicit ...
99+ // AuthorizeImplicit 隐式授权模式授权请求 / Implicit grant authorization request
100+ // redirectURI: 重定向URI / Redirect URI after authorization
101+ // scope: 授权范围 / Requested scope
102+ // state: 状态码,用于防止CSRF攻击 / State parameter for CSRF protection
93103func (c * Client ) AuthorizeImplicit (ctx context.Context , w http.ResponseWriter , redirectURI , scope , state string ) (err error ) {
94104 return c .authorize (ctx , w , TokenKey , redirectURI , scope , state )
95105}
96106
97- // DeviceAuthorization ...
107+ // DeviceAuthorization 设备授权请求 / Device authorization request (RFC 8628)
108+ // scope: 授权范围 / Requested scope
98109func (c * Client ) DeviceAuthorization (ctx context.Context , w http.ResponseWriter , scope string ) (err error ) {
99110 var uri * url.URL
100111 uri , err = url .Parse (c .ServerBaseURL + c .DeviceAuthorizationEndpoint )
@@ -175,7 +186,9 @@ func (c *Client) token(ctx context.Context, grantType string, values url.Values)
175186 return
176187}
177188
178- // TokenResourceOwnerPasswordCredentials ...
189+ // TokenResourceOwnerPasswordCredentials 密码模式获取令牌 / Resource owner password credentials grant
190+ // username: 用户名 / Resource owner username
191+ // password: 密码 / Resource owner password
179192func (c * Client ) TokenResourceOwnerPasswordCredentials (ctx context.Context , username , password string ) (model * TokenResponse , err error ) {
180193 values := url.Values {
181194 UsernameKey : []string {username },
@@ -184,7 +197,8 @@ func (c *Client) TokenResourceOwnerPasswordCredentials(ctx context.Context, user
184197 return c .token (ctx , PasswordKey , values )
185198}
186199
187- // TokenClientCredentials ...
200+ // TokenClientCredentials 客户端凭证模式获取令牌 / Client credentials grant
201+ // scope: 授权范围(可选) / Requested scope (optional)
188202func (c * Client ) TokenClientCredentials (ctx context.Context , scope ... string ) (model * TokenResponse , err error ) {
189203 values := url.Values {}
190204 if len (scope ) > 0 {
@@ -193,15 +207,17 @@ func (c *Client) TokenClientCredentials(ctx context.Context, scope ...string) (m
193207 return c .token (ctx , ClientCredentialsKey , values )
194208}
195209
196- // RefreshToken ...
210+ // RefreshToken 刷新访问令牌 / Refresh access token using refresh token
211+ // refreshToken: 刷新令牌 / Refresh token
197212func (c * Client ) RefreshToken (ctx context.Context , refreshToken string ) (model * TokenResponse , err error ) {
198213 values := url.Values {
199214 RefreshTokenKey : []string {refreshToken },
200215 }
201216 return c .token (ctx , RefreshTokenKey , values )
202217}
203218
204- // TokenDeviceCode ...
219+ // TokenDeviceCode 设备码模式获取令牌 / Exchange device code for access token (RFC 8628)
220+ // deviceCode: 设备码 / Device code received from device authorization
205221func (c * Client ) TokenDeviceCode (ctx context.Context , deviceCode string ) (model * TokenResponse , err error ) {
206222 values := url.Values {
207223 ClientIDKey : []string {c .ID },
@@ -210,7 +226,9 @@ func (c *Client) TokenDeviceCode(ctx context.Context, deviceCode string) (model
210226 return c .token (ctx , DeviceCodeKey , values )
211227}
212228
213- // TokenIntrospect ...
229+ // TokenIntrospect 令牌内省 / Token introspection (RFC 7662)
230+ // token: 要检查的令牌 / Token to introspect
231+ // tokenTypeHint: 令牌类型提示(可选) / Token type hint (optional): access_token or refresh_token
214232func (c * Client ) TokenIntrospect (ctx context.Context , token string , tokenTypeHint ... string ) (introspection * IntrospectionResponse , err error ) {
215233 values := url.Values {
216234 TokenKey : []string {token },
@@ -227,7 +245,9 @@ func (c *Client) TokenIntrospect(ctx context.Context, token string, tokenTypeHin
227245 return
228246}
229247
230- // TokenRevocation token撤销
248+ // TokenRevocation 令牌撤销 / Token revocation (RFC 7009)
249+ // token: 要撤销的令牌 / Token to revoke
250+ // tokenTypeHint: 令牌类型提示(可选) / Token type hint (optional): access_token or refresh_token
231251func (c * Client ) TokenRevocation (ctx context.Context , token string , tokenTypeHint ... string ) (introspection * IntrospectionResponse , err error ) {
232252 values := url.Values {
233253 TokenKey : []string {token },
@@ -263,7 +283,7 @@ func (c *Client) do(ctx context.Context, path string, values url.Values, v inter
263283 }
264284 defer resp .Body .Close ()
265285 var body []byte
266- body , err = ioutil .ReadAll (resp .Body )
286+ body , err = io .ReadAll (resp .Body )
267287 if err != nil {
268288 return
269289 }
0 commit comments