Skip to content

Commit d404d10

Browse files
committed
Passthrough token request
1 parent e165423 commit d404d10

File tree

4 files changed

+47
-39
lines changed

4 files changed

+47
-39
lines changed

README.md

+32-32
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22

33
> An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.
44
5-
[![Build][Build-Status-Image]][Build-Status-Url] [![Codecov][codecov-image]][codecov-url] [![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url] [![License][license-image]][license-url]
5+
[![Build][build-status-image]][build-status-url] [![Codecov][codecov-image]][codecov-url] [![ReportCard][reportcard-image]][reportcard-url] [![GoDoc][godoc-image]][godoc-url] [![License][license-image]][license-url]
66

77
## Protocol Flow
88

9-
``` text
9+
```text
1010
+--------+ +---------------+
1111
| |--(A)- Authorization Request ->| Resource |
1212
| | | Owner |
@@ -30,13 +30,13 @@
3030

3131
### Download and install
3232

33-
``` bash
33+
```bash
3434
go get -u -v github.com/go-oauth2/oauth2/v4/...
3535
```
3636

3737
### Create file `server.go`
3838

39-
``` go
39+
```go
4040
package main
4141

4242
import (
@@ -95,7 +95,7 @@ func main() {
9595

9696
### Build and run
9797

98-
``` bash
98+
```bash
9999
go build server.go
100100

101101
./server
@@ -105,24 +105,24 @@ go build server.go
105105

106106
[http://localhost:9096/token?grant_type=client_credentials&client_id=000000&client_secret=999999&scope=read](http://localhost:9096/token?grant_type=client_credentials&client_id=000000&client_secret=999999&scope=read)
107107

108-
``` json
108+
```json
109109
{
110-
"access_token": "J86XVRYSNFCFI233KXDL0Q",
111-
"expires_in": 7200,
112-
"scope": "read",
113-
"token_type": "Bearer"
110+
"access_token": "J86XVRYSNFCFI233KXDL0Q",
111+
"expires_in": 7200,
112+
"scope": "read",
113+
"token_type": "Bearer"
114114
}
115115
```
116116

117117
## Features
118118

119-
* Easy to use
120-
* Based on the [RFC 6749](https://tools.ietf.org/html/rfc6749) implementation
121-
* Token storage support TTL
122-
* Support custom expiration time of the access token
123-
* Support custom extension field
124-
* Support custom scope
125-
* Support jwt to generate access tokens
119+
- Easy to use
120+
- Based on the [RFC 6749](https://tools.ietf.org/html/rfc6749) implementation
121+
- Token storage support TTL
122+
- Support custom expiration time of the access token
123+
- Support custom extension field
124+
- Support custom scope
125+
- Support jwt to generate access tokens
126126

127127
## Example
128128

@@ -161,28 +161,28 @@ if !ok || !token.Valid {
161161

162162
## Store Implements
163163

164-
* [BuntDB](https://github.com/tidwall/buntdb)(default store)
165-
* [Redis](https://github.com/go-oauth2/redis)
166-
* [MongoDB](https://github.com/go-oauth2/mongo)
167-
* [MySQL](https://github.com/go-oauth2/mysql)
168-
* [MySQL (Provides both client and token store)](https://github.com/imrenagi/go-oauth2-mysql)
169-
* [PostgreSQL](https://github.com/vgarvardt/go-oauth2-pg)
170-
* [DynamoDB](https://github.com/contamobi/go-oauth2-dynamodb)
171-
* [XORM](https://github.com/techknowlogick/go-oauth2-xorm)
172-
* [XORM (MySQL, client and token store)](https://github.com/rainlay/go-oauth2-xorm)
173-
* [GORM](https://github.com/techknowlogick/go-oauth2-gorm)
174-
* [Firestore](https://github.com/tslamic/go-oauth2-firestore)
164+
- [BuntDB](https://github.com/tidwall/buntdb)(default store)
165+
- [Redis](https://github.com/go-oauth2/redis)
166+
- [MongoDB](https://github.com/go-oauth2/mongo)
167+
- [MySQL](https://github.com/go-oauth2/mysql)
168+
- [MySQL (Provides both client and token store)](https://github.com/imrenagi/go-oauth2-mysql)
169+
- [PostgreSQL](https://github.com/vgarvardt/go-oauth2-pg)
170+
- [DynamoDB](https://github.com/contamobi/go-oauth2-dynamodb)
171+
- [XORM](https://github.com/techknowlogick/go-oauth2-xorm)
172+
- [XORM (MySQL, client and token store)](https://github.com/rainlay/go-oauth2-xorm)
173+
- [GORM](https://github.com/techknowlogick/go-oauth2-gorm)
174+
- [Firestore](https://github.com/tslamic/go-oauth2-firestore)
175175

176176
## Handy Utilities
177177

178-
* [OAuth2 Proxy Logger (Debug utility that proxies interfaces and logs)](https://github.com/aubelsb2/oauth2-logger-proxy)
178+
- [OAuth2 Proxy Logger (Debug utility that proxies interfaces and logs)](https://github.com/aubelsb2/oauth2-logger-proxy)
179179

180180
## MIT License
181181

182-
Copyright (c) 2016 Lyric
182+
Copyright (c) 2016 Lyric
183183

184-
[Build-Status-Url]: https://travis-ci.org/go-oauth2/oauth2
185-
[Build-Status-Image]: https://travis-ci.org/go-oauth2/oauth2.svg?branch=master
184+
[build-status-url]: https://travis-ci.org/go-oauth2/oauth2
185+
[build-status-image]: https://travis-ci.org/go-oauth2/oauth2.svg?branch=master
186186
[codecov-url]: https://codecov.io/gh/go-oauth2/oauth2
187187
[codecov-image]: https://codecov.io/gh/go-oauth2/oauth2/branch/master/graph/badge.svg
188188
[reportcard-url]: https://goreportcard.com/report/github.com/go-oauth2/oauth2/v4

server/handler.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type (
1616
ClientAuthorizedHandler func(clientID string, grant oauth2.GrantType) (allowed bool, err error)
1717

1818
// ClientScopeHandler check the client allows to use scope
19-
ClientScopeHandler func(clientID, scope string) (allowed bool, err error)
19+
ClientScopeHandler func(tgr *oauth2.TokenGenerateRequest) (allowed bool, err error)
2020

2121
// UserAuthorizationHandler get user id from request authorization
2222
UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error)
@@ -25,9 +25,9 @@ type (
2525
PasswordAuthorizationHandler func(username, password string) (userID string, err error)
2626

2727
// RefreshingScopeHandler check the scope of the refreshing token
28-
RefreshingScopeHandler func(newScope, oldScope string) (allowed bool, err error)
28+
RefreshingScopeHandler func(tgr *oauth2.TokenGenerateRequest, oldScope string) (allowed bool, err error)
2929

30-
//RefreshingValidationHandler check if refresh_token is still valid. eg no revocation or other
30+
// RefreshingValidationHandler check if refresh_token is still valid. eg no revocation or other
3131
RefreshingValidationHandler func(ti oauth2.TokenInfo) (allowed bool, err error)
3232

3333
// ResponseErrorHandler response error handing

server/server.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,15 @@ func (s *Server) GetAuthorizeToken(ctx context.Context, req *AuthorizeRequest) (
214214

215215
// check the client allows the authorized scope
216216
if fn := s.ClientScopeHandler; fn != nil {
217-
allowed, err := fn(req.ClientID, req.Scope)
217+
tgr := &oauth2.TokenGenerateRequest{
218+
ClientID: req.ClientID,
219+
UserID: req.UserID,
220+
RedirectURI: req.RedirectURI,
221+
Scope: req.Scope,
222+
AccessTokenExp: req.AccessTokenExp,
223+
Request: req.Request,
224+
}
225+
allowed, err := fn(tgr)
218226
if err != nil {
219227
return nil, err
220228
} else if !allowed {
@@ -402,7 +410,7 @@ func (s *Server) GetAccessToken(ctx context.Context, gt oauth2.GrantType, tgr *o
402410
return ti, nil
403411
case oauth2.PasswordCredentials, oauth2.ClientCredentials:
404412
if fn := s.ClientScopeHandler; fn != nil {
405-
allowed, err := fn(tgr.ClientID, tgr.Scope)
413+
allowed, err := fn(tgr)
406414
if err != nil {
407415
return nil, err
408416
} else if !allowed {
@@ -421,7 +429,7 @@ func (s *Server) GetAccessToken(ctx context.Context, gt oauth2.GrantType, tgr *o
421429
return nil, err
422430
}
423431

424-
allowed, err := scopeFn(scope, rti.GetScope())
432+
allowed, err := scopeFn(tgr, rti.GetScope())
425433
if err != nil {
426434
return nil, err
427435
} else if !allowed {

server/server_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ func TestClientCredentials(t *testing.T) {
306306
srv.SetAuthorizeScopeHandler(func(w http.ResponseWriter, r *http.Request) (scope string, err error) {
307307
return
308308
})
309-
srv.SetClientScopeHandler(func(clientID, scope string) (allowed bool, err error) {
309+
srv.SetClientScopeHandler(func(tgr *oauth2.TokenGenerateRequest) (allowed bool, err error) {
310310
allowed = true
311311
return
312312
})

0 commit comments

Comments
 (0)