Skip to content

Commit 5e9ac53

Browse files
authored
Merge pull request #57 from LyricTian/develop
fixed the token check
2 parents 81db4aa + fc86c13 commit 5e9ac53

File tree

3 files changed

+21
-18
lines changed

3 files changed

+21
-18
lines changed

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# Golang OAuth 2.0
22

3-
> An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.
3+
> An open protocol to allow secure authorization in a simple and standard method from web, mobile and desktop applications.
44
5-
[![License][License-Image]][License-Url] [![ReportCard][ReportCard-Image]][ReportCard-Url] [![Build][Build-Status-Image]][Build-Status-Url] [![Coverage][Coverage-Image]][Coverage-Url] [![GoDoc][GoDoc-Image]][GoDoc-Url] [![Release][Release-Image]][Release-Url]
5+
[![License][License-Image]][License-Url] [![ReportCard][ReportCard-Image]][ReportCard-Url] [![Build][Build-Status-Image]][Build-Status-Url] [![Coverage][Coverage-Image]][Coverage-Url] [![GoDoc][GoDoc-Image]][GoDoc-Url]
66

77
## Protocol Flow
88

9-
```
9+
``` text
1010
+--------+ +---------------+
1111
| |--(A)- Authorization Request ->| Resource |
1212
| | | Owner |
@@ -31,7 +31,7 @@
3131
### Download and install
3232

3333
``` bash
34-
$ go get -u gopkg.in/oauth2.v3/...
34+
go get -u -v gopkg.in/oauth2.v3/...
3535
```
3636

3737
### Create file `server.go`
@@ -96,15 +96,14 @@ func main() {
9696
### Build and run
9797

9898
``` bash
99-
$ go build server.go
100-
$ ./server
99+
go build server.go
100+
101+
./server
101102
```
102103

103104
### Open in your web browser
104105

105-
```
106-
http://localhost:9096/token?grant_type=client_credentials&client_id=000000&client_secret=999999&scope=read
107-
```
106+
[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)
108107

109108
``` json
110109
{
@@ -138,16 +137,12 @@ Simulation examples of authorization code model, please check [example](/example
138137

139138
## MIT License
140139

141-
```
142-
Copyright (c) 2016 Lyric
143-
```
140+
Copyright (c) 2016 Lyric
144141

145142
[License-Url]: http://opensource.org/licenses/MIT
146143
[License-Image]: https://img.shields.io/npm/l/express.svg
147144
[Build-Status-Url]: https://travis-ci.org/go-oauth2/oauth2
148145
[Build-Status-Image]: https://travis-ci.org/go-oauth2/oauth2.svg?branch=master
149-
[Release-Url]: https://github.com/go-oauth2/oauth2/releases/tag/v3.7.0
150-
[Release-image]: http://img.shields.io/badge/release-v3.7.0-1eb0fc.svg
151146
[ReportCard-Url]: https://goreportcard.com/report/gopkg.in/oauth2.v3
152147
[ReportCard-Image]: https://goreportcard.com/badge/gopkg.in/oauth2.v3
153148
[GoDoc-Url]: https://godoc.org/gopkg.in/oauth2.v3

manage/manage_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,14 @@ func testManager(tgr *oauth2.TokenGenerateRequest, manager oauth2.Manager) {
7373
So(err, ShouldBeNil)
7474
So(ainfo.GetClientID(), ShouldEqual, atParams.ClientID)
7575

76+
arinfo, err := manager.LoadRefreshToken(accessToken)
77+
So(err, ShouldNotBeNil)
78+
So(arinfo, ShouldBeNil)
79+
80+
rainfo, err := manager.LoadAccessToken(refreshToken)
81+
So(err, ShouldNotBeNil)
82+
So(rainfo, ShouldBeNil)
83+
7684
rinfo, err := manager.LoadRefreshToken(refreshToken)
7785
So(err, ShouldBeNil)
7886
So(rinfo.GetClientID(), ShouldEqual, atParams.ClientID)

manage/manager.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func (m *Manager) GenerateAuthToken(rt oauth2.ResponseType, tgr *oauth2.TokenGen
220220
ti.SetRefreshExpiresIn(icfg.RefreshTokenExp)
221221
}
222222
}
223-
223+
224224
err = stor.Create(ti)
225225
if err != nil {
226226
return
@@ -240,7 +240,7 @@ func (m *Manager) getAuthorizationCode(code string) (info oauth2.TokenInfo, err
240240
if terr != nil {
241241
err = terr
242242
return
243-
} else if ti == nil || ti.GetCodeCreateAt().Add(ti.GetCodeExpiresIn()).Before(time.Now()) {
243+
} else if ti == nil || ti.GetCode() != code || ti.GetCodeCreateAt().Add(ti.GetCodeExpiresIn()).Before(time.Now()) {
244244
err = errors.ErrInvalidAuthorizeCode
245245
return
246246
}
@@ -474,7 +474,7 @@ func (m *Manager) LoadAccessToken(access string) (info oauth2.TokenInfo, err err
474474
if terr != nil {
475475
err = terr
476476
return
477-
} else if ti == nil {
477+
} else if ti == nil || ti.GetAccess() != access {
478478
err = errors.ErrInvalidAccessToken
479479
return
480480
} else if ti.GetRefresh() != "" && ti.GetRefreshCreateAt().Add(ti.GetRefreshExpiresIn()).Before(ct) {
@@ -502,7 +502,7 @@ func (m *Manager) LoadRefreshToken(refresh string) (info oauth2.TokenInfo, err e
502502
if terr != nil {
503503
err = terr
504504
return
505-
} else if ti == nil {
505+
} else if ti == nil || ti.GetRefresh() != refresh {
506506
err = errors.ErrInvalidRefreshToken
507507
return
508508
} else if ti.GetRefreshCreateAt().Add(ti.GetRefreshExpiresIn()).Before(time.Now()) {

0 commit comments

Comments
 (0)