Skip to content

Commit 34366d8

Browse files
committed
update example
1 parent 608452c commit 34366d8

File tree

3 files changed

+53
-38
lines changed

3 files changed

+53
-38
lines changed

.travis.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ language: go
22
sudo: false
33
go_import_path: gopkg.in/oauth2.v3
44
go:
5-
- 1.7
5+
- 1.9
66
before_install:
7-
- go get github.com/mattn/goveralls
7+
- go get -t -v ./...
8+
89
script:
9-
- $HOME/gopath/bin/goveralls -service=travis-ci
10+
- go test -race -coverprofile=coverage.txt -covermode=atomic
11+
12+
after_success:
13+
- bash <(curl -s https://codecov.io/bash)

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
> 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]
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

@@ -140,13 +140,13 @@ Simulation examples of authorization code model, please check [example](/example
140140

141141
Copyright (c) 2016 Lyric
142142

143-
[License-Url]: http://opensource.org/licenses/MIT
144-
[License-Image]: https://img.shields.io/npm/l/express.svg
145143
[Build-Status-Url]: https://travis-ci.org/go-oauth2/oauth2
146144
[Build-Status-Image]: https://travis-ci.org/go-oauth2/oauth2.svg?branch=master
147-
[ReportCard-Url]: https://goreportcard.com/report/gopkg.in/oauth2.v3
148-
[ReportCard-Image]: https://goreportcard.com/badge/gopkg.in/oauth2.v3
149-
[GoDoc-Url]: https://godoc.org/gopkg.in/oauth2.v3
150-
[GoDoc-Image]: https://godoc.org/gopkg.in/oauth2.v3?status.svg
151-
[Coverage-Url]: https://coveralls.io/github/go-oauth2/oauth2?branch=master
152-
[Coverage-Image]: https://coveralls.io/repos/github/go-oauth2/oauth2/badge.svg?branch=master
145+
[codecov-url]: https://codecov.io/gh/go-oauth2/oauth2
146+
[codecov-image]: https://codecov.io/gh/go-oauth2/oauth2/branch/master/graph/badge.svg
147+
[reportcard-url]: https://goreportcard.com/report/gopkg.in/oauth2.v3
148+
[reportcard-image]: https://goreportcard.com/badge/gopkg.in/oauth2.v3
149+
[godoc-url]: https://godoc.org/gopkg.in/oauth2.v3
150+
[godoc-image]: https://godoc.org/gopkg.in/oauth2.v3?status.svg
151+
[license-url]: http://opensource.org/licenses/MIT
152+
[license-image]: https://img.shields.io/npm/l/express.svg

example/server/server.go

Lines changed: 37 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,14 @@ import (
66
"net/url"
77
"os"
88

9+
"github.com/go-session/session"
910
"gopkg.in/oauth2.v3/errors"
1011
"gopkg.in/oauth2.v3/manage"
1112
"gopkg.in/oauth2.v3/models"
1213
"gopkg.in/oauth2.v3/server"
1314
"gopkg.in/oauth2.v3/store"
14-
"gopkg.in/session.v1"
1515
)
1616

17-
var (
18-
globalSessions *session.Manager
19-
)
20-
21-
func init() {
22-
globalSessions, _ = session.NewManager("memory", `{"cookieName":"gosessionid","gclifetime":3600}`)
23-
go globalSessions.GC()
24-
}
25-
2617
func main() {
2718
manager := manage.NewDefaultManager()
2819
// token store
@@ -70,30 +61,40 @@ func main() {
7061
}
7162

7263
func userAuthorizeHandler(w http.ResponseWriter, r *http.Request) (userID string, err error) {
73-
us, err := globalSessions.SessionStart(w, r)
74-
uid := us.Get("UserID")
75-
if uid == nil {
64+
store, err := session.Start(nil, w, r)
65+
if err != nil {
66+
return
67+
}
68+
69+
uid, ok := store.Get("UserID")
70+
if !ok {
7671
if r.Form == nil {
7772
r.ParseForm()
7873
}
79-
us.Set("ReturnUri", r.Form)
74+
store.Set("ReturnUri", r.Form)
75+
store.Save()
76+
8077
w.Header().Set("Location", "/login")
8178
w.WriteHeader(http.StatusFound)
8279
return
8380
}
8481
userID = uid.(string)
85-
us.Delete("UserID")
82+
store.Delete("UserID")
83+
store.Save()
8684
return
8785
}
8886

8987
func loginHandler(w http.ResponseWriter, r *http.Request) {
88+
store, err := session.Start(nil, w, r)
89+
if err != nil {
90+
http.Error(w, err.Error(), http.StatusInternalServerError)
91+
return
92+
}
93+
9094
if r.Method == "POST" {
91-
us, err := globalSessions.SessionStart(w, r)
92-
if err != nil {
93-
http.Error(w, err.Error(), http.StatusInternalServerError)
94-
return
95-
}
96-
us.Set("LoggedInUserID", "000000")
95+
store.Set("LoggedInUserID", "000000")
96+
store.Save()
97+
9798
w.Header().Set("Location", "/auth")
9899
w.WriteHeader(http.StatusFound)
99100
return
@@ -102,25 +103,35 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
102103
}
103104

104105
func authHandler(w http.ResponseWriter, r *http.Request) {
105-
us, err := globalSessions.SessionStart(w, r)
106+
store, err := session.Start(nil, w, r)
106107
if err != nil {
107108
http.Error(w, err.Error(), http.StatusInternalServerError)
108109
return
109110
}
110-
if us.Get("LoggedInUserID") == nil {
111+
112+
if _, ok := store.Get("LoggedInUserID"); !ok {
111113
w.Header().Set("Location", "/login")
112114
w.WriteHeader(http.StatusFound)
113115
return
114116
}
117+
115118
if r.Method == "POST" {
116-
form := us.Get("ReturnUri").(url.Values)
119+
var form url.Values
120+
if v, ok := store.Get("ReturnUri"); ok {
121+
form = v.(url.Values)
122+
}
117123
u := new(url.URL)
118124
u.Path = "/authorize"
119125
u.RawQuery = form.Encode()
120126
w.Header().Set("Location", u.String())
121127
w.WriteHeader(http.StatusFound)
122-
us.Delete("Form")
123-
us.Set("UserID", us.Get("LoggedInUserID"))
128+
store.Delete("Form")
129+
130+
if v, ok := store.Get("LoggedInUserID"); ok {
131+
store.Set("UserID", v)
132+
}
133+
store.Save()
134+
124135
return
125136
}
126137
outputHTML(w, r, "static/auth.html")

0 commit comments

Comments
 (0)