Skip to content

Commit fae732f

Browse files
committed
Add some views
1 parent 05512bd commit fae732f

File tree

11 files changed

+141
-16
lines changed

11 files changed

+141
-16
lines changed

api/begin.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package api
2+
3+
import (
4+
"net/http"
5+
"net/url"
6+
7+
"github.com/anduintransaction/oauth-proxy/proxy"
8+
"github.com/anduintransaction/oauth-proxy/service"
9+
"gottb.io/goru"
10+
"gottb.io/goru/errors"
11+
"gottb.io/goru/log"
12+
"gottb.io/gorux"
13+
)
14+
15+
func Begin(ctx *goru.Context) {
16+
p := proxy.GetProxy(ctx.Request.Host)
17+
if p == nil {
18+
gorux.ResponseJSON(ctx, http.StatusOK, Error("Anduin OAUTH proxy version "+service.Version()))
19+
return
20+
}
21+
user := service.CheckSession(ctx)
22+
if user != nil {
23+
goru.Redirect(ctx, "/")
24+
return
25+
}
26+
requestPath := gorux.Query(ctx, "request-path")
27+
if requestPath == "" {
28+
requestPath = "/"
29+
}
30+
requestURL, err := url.Parse(requestPath)
31+
if err != nil {
32+
log.Error(errors.Wrap(err))
33+
gorux.ResponseJSON(ctx, http.StatusBadRequest, Error("Invalid request URL"))
34+
}
35+
ctx.Request.URL = requestURL
36+
service.DoRedirect(ctx, p)
37+
}

api/callback.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"net/http"
54
"net/url"
65

76
"github.com/anduintransaction/oauth-proxy/provider"
@@ -15,40 +14,40 @@ import (
1514
func Callback(ctx *goru.Context) {
1615
stateName := gorux.Query(ctx, "state")
1716
if stateName == "" {
18-
gorux.ResponseJSON(ctx, http.StatusBadRequest, Error("state is required"))
17+
RenderError(ctx, "State is required")
1918
return
2019
}
2120
state := proxy.GetState(stateName)
2221
if state == nil {
23-
gorux.ResponseJSON(ctx, http.StatusUnauthorized, Error("state not found or expired"))
22+
RenderError(ctx, "State not found or expired")
2423
return
2524
}
2625
prov := provider.GetProvider(state.Proxy.Provider)
2726
if prov == nil {
2827
log.Errorf("Provider not found: %s", state.Proxy.Provider)
29-
gorux.ResponseJSON(ctx, http.StatusInternalServerError, InternalServerError)
28+
RenderError(ctx, InternalServerError.Message)
3029
return
3130
}
3231
code := gorux.Query(ctx, "code")
3332
if code == "" {
3433
log.Errorf("Error found in callback: %s", ctx.Request.URL.String())
35-
gorux.ResponseJSON(ctx, http.StatusBadRequest, Error(prov.ErrorString(ctx.Request)))
34+
RenderError(ctx, prov.ErrorString(ctx.Request))
3635
return
3736
}
3837
token, err := prov.RequestToken(state, code)
3938
if err != nil {
4039
log.Error(err)
41-
gorux.ResponseJSON(ctx, http.StatusInternalServerError, Error("cannot request token"))
40+
RenderError(ctx, "Cannot request token")
4241
return
4342
}
4443
user, err := prov.VerifyUser(state, token)
4544
if err != nil {
4645
log.Error(err)
47-
gorux.ResponseJSON(ctx, http.StatusInternalServerError, Error("cannot verify user"))
46+
RenderError(ctx, "Cannot verify user")
4847
return
4948
}
5049
if user == nil {
51-
gorux.ResponseJSON(ctx, http.StatusUnauthorized, Error("unauthorized user"))
50+
RenderError(ctx, "Unauthorized user")
5251
return
5352
}
5453

api/error.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
package api
22

3+
import (
4+
"net/http"
5+
6+
"github.com/anduintransaction/oauth-proxy/views"
7+
"gottb.io/goru"
8+
"gottb.io/gorux"
9+
)
10+
311
type ErrorMessage struct {
412
Message string `json:"message"`
513
}
@@ -9,3 +17,11 @@ func Error(message string) *ErrorMessage {
917
}
1018

1119
var InternalServerError = Error("internal server error")
20+
21+
func RenderError(ctx *goru.Context, message string) {
22+
b, err := views.Error.Render(message)
23+
if err != nil {
24+
gorux.ResponseJSON(ctx, http.StatusInternalServerError, InternalServerError)
25+
}
26+
goru.Ok(ctx, b)
27+
}

api/login.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@ import (
1818
func Login(ctx *goru.Context) {
1919
stateName := gorux.Query(ctx, "state")
2020
if stateName == "" {
21-
gorux.ResponseJSON(ctx, http.StatusBadRequest, Error("state is required"))
21+
RenderError(ctx, "State is required")
2222
return
2323
}
2424
state := proxy.AcquireState(stateName)
2525
if state == nil {
26-
gorux.ResponseJSON(ctx, http.StatusUnauthorized, Error("state not found or expired"))
26+
RenderError(ctx, "State not found or expired")
2727
return
2828
}
2929
if state.User == nil {
30-
gorux.ResponseJSON(ctx, http.StatusUnauthorized, Error("user was not authenticated"))
30+
RenderError(ctx, "User was not authenticated")
3131
return
3232
}
3333
session := &proxy.Session{
@@ -37,13 +37,13 @@ func Login(ctx *goru.Context) {
3737
cookieContent, err := json.Marshal(session)
3838
if err != nil {
3939
log.Error(errors.Wrap(err))
40-
gorux.ResponseJSON(ctx, http.StatusInternalServerError, InternalServerError)
40+
RenderError(ctx, InternalServerError.Message)
4141
return
4242
}
4343
encryptedContent, err := crypto.Encrypt(cookieContent)
4444
if err != nil {
4545
log.Error(err)
46-
gorux.ResponseJSON(ctx, http.StatusInternalServerError, InternalServerError)
46+
RenderError(ctx, InternalServerError.Message)
4747
return
4848
}
4949
goru.SetCookie(ctx, &http.Cookie{

api/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55

66
"github.com/anduintransaction/oauth-proxy/proxy"
77
"github.com/anduintransaction/oauth-proxy/service"
8+
"github.com/anduintransaction/oauth-proxy/views"
89
"gottb.io/goru"
10+
"gottb.io/goru/log"
911
"gottb.io/gorux"
1012
)
1113

@@ -20,7 +22,12 @@ func Main(ctx *goru.Context) {
2022
service.ReverseProxy(ctx, p, user)
2123
return
2224
}
23-
service.DoRedirect(ctx, p)
25+
content, err := views.Index.Render(ctx.Request.URL.String())
26+
if err != nil {
27+
log.Error(err)
28+
gorux.ResponseJSON(ctx, http.StatusInternalServerError, InternalServerError)
29+
}
30+
goru.Unauthorized(ctx, content)
2431
}
2532

2633
func Favicon(ctx *goru.Context) {

build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env bash
22

3-
RELEASE=0.1.0
3+
RELEASE=0.2.0
44
dist=dist
55
bin=oauth-proxy
66

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:generate goru generate view
12
package main
23

34
import (
@@ -7,13 +8,15 @@ import (
78
"gottb.io/goru/crypto"
89
"gottb.io/goru/log"
910
"gottb.io/goru/session"
11+
_ "gottb.io/gorux/functions"
1012
)
1113

1214
func main() {
1315
r := goru.NewRouter()
1416
r.Any("/**", goru.HandlerFunc(api.Main))
1517
r.Get("/oauth2/callback", goru.HandlerFunc(api.Callback))
1618
r.Get("/oauth2/login", goru.HandlerFunc(api.Login))
19+
r.Get("/oauth2/begin", goru.HandlerFunc(api.Begin))
1720
r.Get("/favicon.ico", goru.HandlerFunc(api.Favicon))
1821

1922
goru.StartWith(log.Start)

proxy/state.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ func (m *stateMap) acquire(name string) *State {
8585
}
8686

8787
func (m *stateMap) getUnsafe(name string) *State {
88+
log.Debugf("Getting state %s", name)
8889
state := m.white[name]
8990
if state != nil {
9091
log.Debugf("State found from white: %s to %s", name, state.Request.URL.String())

service/misc.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func Version() string {
9-
return "0.1.0"
9+
return "0.2.0"
1010
}
1111

1212
func generateRandomState() (string, error) {

views/error.tmpl.html

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//func(message string)
2+
<!DOCTYPE HTML>
3+
</html>
4+
<head>
5+
<meta charset="utf8">
6+
<title>Anduin Anthentication</title>
7+
{{css "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"}}
8+
<style>
9+
.container {
10+
padding-top: 150px;
11+
}
12+
</style>
13+
</head>
14+
<body>
15+
<div class="container">
16+
<div class="row">
17+
<div class="col-md-8 col-md-offset-2">
18+
<h2 class="text-danger text-center">Something wrong!</h2>
19+
<div class="alert alert-danger" role="alert">
20+
<span class="glyphicon glyphicon-exclamation-sign"></span> {{$message}}
21+
</div>
22+
</div>
23+
</div>
24+
</div>
25+
</body>
26+
</html>

0 commit comments

Comments
 (0)