Skip to content

Commit 228167f

Browse files
committed
Merge branch 'sorah-customizable-oauth2-path'
Conflicts: config_test.go
2 parents 8b5e03d + 4678925 commit 228167f

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

conf.go

+24
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"errors"
55
"gopkg.in/yaml.v1"
66
"io/ioutil"
7+
"github.com/martini-contrib/oauth2"
78
)
89

910
const (
@@ -16,6 +17,7 @@ type Conf struct {
1617
Auth AuthConf `yaml:"auth"`
1718
Restrictions []string `yaml:"restrictions"`
1819
Proxies []ProxyConf `yaml:"proxy"`
20+
Paths PathConf `yaml:"paths"`
1921
Htdocs string `yaml:"htdocs"`
2022
}
2123

@@ -50,6 +52,13 @@ type ProxyConf struct {
5052
Host string `yaml:"host"`
5153
}
5254

55+
type PathConf struct {
56+
Login string `yaml:"login"`
57+
Logout string `yaml:"logout"`
58+
Callback string `yaml:"callback"`
59+
Error string `yaml:"error"`
60+
}
61+
5362
func ParseConf(path string) (*Conf, error) {
5463
data, err := ioutil.ReadFile(path)
5564
if err != nil {
@@ -94,3 +103,18 @@ func ParseConf(path string) (*Conf, error) {
94103

95104
return c, nil
96105
}
106+
107+
func (c *Conf) SetOAuth2Paths() {
108+
if c.Paths.Login != "" {
109+
oauth2.PathLogin = c.Paths.Login
110+
}
111+
if c.Paths.Logout != "" {
112+
oauth2.PathLogout = c.Paths.Logout
113+
}
114+
if c.Paths.Callback != "" {
115+
oauth2.PathCallback = c.Paths.Callback
116+
}
117+
if c.Paths.Error != "" {
118+
oauth2.PathError = c.Paths.Error
119+
}
120+
}

config_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"io/ioutil"
55
"os"
66
"testing"
7+
"github.com/martini-contrib/oauth2"
78
)
89

910
func TestParse(t *testing.T) {
@@ -202,3 +203,57 @@ proxy:
202203
t.Errorf("unexpected proxy[1]: %#v", ifdb)
203204
}
204205
}
206+
207+
func TestPathConf(t *testing.T) {
208+
f, err := ioutil.TempFile("", "")
209+
if err != nil {
210+
t.Error(err)
211+
}
212+
defer func() {
213+
f.Close()
214+
os.Remove(f.Name())
215+
}()
216+
217+
data := `---
218+
address: ":9999"
219+
220+
auth:
221+
session:
222+
key: secret
223+
224+
info:
225+
service: 'github'
226+
client_id: 'secret client id'
227+
client_secret: 'secret client secret'
228+
redirect_url: 'http://example.com/_gate_callback'
229+
230+
paths:
231+
login: "/_gate_login"
232+
logout: "/_gate_logout"
233+
callback: "/_gate_callback"
234+
error: "/_gate_error"
235+
`
236+
if err := ioutil.WriteFile(f.Name(), []byte(data), 0644); err != nil {
237+
t.Error(err)
238+
}
239+
240+
conf, err := ParseConf(f.Name())
241+
if err != nil {
242+
t.Error(err)
243+
}
244+
245+
conf.SetOAuth2Paths()
246+
247+
if oauth2.PathLogin != "/_gate_login" {
248+
t.Errorf("unexpected oauth2.PathLogin: %s", oauth2.PathLogin)
249+
}
250+
if oauth2.PathLogout != "/_gate_logout" {
251+
t.Errorf("unexpected oauth2.PathLogout: %s", oauth2.PathLogout)
252+
}
253+
if oauth2.PathCallback != "/_gate_callback" {
254+
t.Errorf("unexpected oauth2.PathCallback: %s", oauth2.PathCallback)
255+
}
256+
if oauth2.PathError != "/_gate_error" {
257+
t.Errorf("unexpected oauth2.PathError: %s", oauth2.PathError)
258+
}
259+
}

main.go

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func main() {
1717
panic(err)
1818
}
1919

20+
conf.SetOAuth2Paths()
21+
2022
server := NewServer(conf)
2123
log.Fatal(server.Run())
2224
}

0 commit comments

Comments
 (0)