Skip to content

Commit 5804abf

Browse files
author
Renz Albert Galang
committed
added cors & lbo_auth plugin
1 parent 5919e5d commit 5804abf

File tree

10 files changed

+754
-2
lines changed

10 files changed

+754
-2
lines changed

.idea/micro.iml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/workspace.xml

+342
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ go 1.12
44

55
require (
66
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e
7-
github.com/coreos/etcd v3.3.13+incompatible // indirect
87
github.com/golang/protobuf v1.3.1
98
github.com/google/uuid v1.1.1
109
github.com/gorilla/mux v1.7.2
1110
github.com/micro/cli v0.2.0
1211
github.com/micro/go-micro v1.7.1-0.20190710205448-382fbecd4098
12+
github.com/orangeapps/lbo-endgame v0.0.0
1313
github.com/serenize/snaker v0.0.0-20171204205717-a683aaf2d516
1414
github.com/xlab/treeprint v0.0.0-20181112141820-a009c3971eca
15-
go.etcd.io/etcd v3.3.13+incompatible // indirect
1615
golang.org/x/net v0.0.0-20190620200207-3b0461eec859
1716
google.golang.org/grpc v1.21.1
1817
)
18+
19+
replace github.com/orangeapps/lbo-endgame v0.0.0 => /Users/rarg/Work/lbo-endgame

go.sum

+284
Large diffs are not rendered by default.

lbo/auth/auth.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package auth
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"github.com/micro/cli"
8+
"github.com/micro/go-micro/client"
9+
"github.com/micro/micro/plugin"
10+
proto "github.com/orangeapps/lbo-endgame/c/auth/api"
11+
"io/ioutil"
12+
"log"
13+
"net/http"
14+
)
15+
16+
type lBOAuth struct {}
17+
18+
func NewLBOAuth() plugin.Plugin {
19+
return &lBOAuth{}
20+
}
21+
22+
func (a *lBOAuth) Flags() []cli.Flag {
23+
return []cli.Flag{
24+
cli.StringFlag{
25+
Name: "lbo_auth",
26+
Usage: "Enable authentication for lbo-endgame platform.",
27+
},
28+
}
29+
}
30+
31+
func (g *lBOAuth) Commands() []cli.Command {
32+
return nil
33+
}
34+
35+
func (a *lBOAuth) Handler() plugin.Handler {
36+
return func(h http.Handler) http.Handler {
37+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
38+
b, err := ioutil.ReadAll(r.Body)
39+
if err != nil {
40+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
41+
return
42+
}
43+
c := ioutil.NopCloser(bytes.NewBuffer(b))
44+
decoder := json.NewDecoder(c)
45+
var t map[string]interface{}
46+
err = decoder.Decode(&t)
47+
if err != nil {
48+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
49+
return
50+
}
51+
method, ok := t["method"]
52+
if !ok {
53+
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
54+
return
55+
}
56+
if method != "AuthService.Authenticate" && method != "AuthService.Validate" {
57+
token := r.Header.Get("Token")
58+
if token == "" {
59+
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
60+
return
61+
}
62+
log.Print(token)
63+
protoCli := proto.NewAuthService("c.auth", client.NewClient())
64+
_, err := protoCli.Validate(context.Background(), &proto.ValidateReq{
65+
AccessToken: token,
66+
})
67+
if err != nil {
68+
log.Print(err)
69+
http.Error(w, http.StatusText(http.StatusForbidden), http.StatusForbidden)
70+
return
71+
}
72+
}
73+
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
74+
h.ServeHTTP(w, r)
75+
})
76+
}
77+
}
78+
79+
func (p *lBOAuth) Init(ctx *cli.Context) error {
80+
return nil
81+
}
82+
83+
func (p *lBOAuth) String() string {
84+
return "lbo_auth"
85+
}

micro

26.3 MB
Binary file not shown.

plugin.go

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package main
2+
3+
import (
4+
"github.com/micro/go-plugins/micro/cors"
5+
"github.com/micro/micro/lbo/auth"
6+
"github.com/micro/micro/plugin"
7+
)
8+
9+
func init() {
10+
plugin.Register(auth.NewLBOAuth())
11+
plugin.Register(cors.NewPlugin())
12+
}

0 commit comments

Comments
 (0)