Quellcode durchsuchen

feat(rest): support cors

Mustafa Arici vor 8 Jahren
Ursprung
Commit
afcc46a2df
5 geänderte Dateien mit 48 neuen und 39 gelöschten Zeilen
  1. 21 30
      api/pb/auth.pb.go
  2. 1 2
      api/pb/auth.proto
  3. 26 3
      api/rest.go
  4. 0 0
      bindata/bindata.go
  5. 0 4
      template/auth.swagger.json

+ 21 - 30
api/pb/auth.pb.go

@@ -51,8 +51,7 @@ func (m *AuthAuthenticateRequest) GetPassword() string {
 }
 
 type AuthStatusResponse struct {
-	IsAuthenticated bool               `protobuf:"varint,1,opt,name=is_authenticated,json=isAuthenticated" json:"is_authenticated,omitempty"`
-	User            *UserResponse_User `protobuf:"bytes,2,opt,name=user" json:"user,omitempty"`
+	User *UserResponse_User `protobuf:"bytes,1,opt,name=user" json:"user,omitempty"`
 }
 
 func (m *AuthStatusResponse) Reset()                    { *m = AuthStatusResponse{} }
@@ -60,13 +59,6 @@ func (m *AuthStatusResponse) String() string            { return proto.CompactTe
 func (*AuthStatusResponse) ProtoMessage()               {}
 func (*AuthStatusResponse) Descriptor() ([]byte, []int) { return fileDescriptor3, []int{2} }
 
-func (m *AuthStatusResponse) GetIsAuthenticated() bool {
-	if m != nil {
-		return m.IsAuthenticated
-	}
-	return false
-}
-
 func (m *AuthStatusResponse) GetUser() *UserResponse_User {
 	if m != nil {
 		return m.User
@@ -205,25 +197,24 @@ var _AuthService_serviceDesc = grpc.ServiceDesc{
 func init() { proto.RegisterFile("auth.proto", fileDescriptor3) }
 
 var fileDescriptor3 = []byte{
-	// 306 bytes of a gzipped FileDescriptorProto
-	0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x6c, 0x91, 0xc1, 0x4e, 0x32, 0x31,
-	0x14, 0x85, 0x33, 0xe4, 0xff, 0x09, 0x5e, 0x4c, 0x90, 0x2a, 0x42, 0x2a, 0x0b, 0xd2, 0x95, 0xb8,
-	0x60, 0x14, 0x77, 0xee, 0x7c, 0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0x76, 0x93, 0x5c, 0x94, 0xbd,
+	0xf0, 0xc3, 0xae, 0x7f, 0x03, 0x00, 0x00, 0xff, 0xff, 0x4d, 0x24, 0x8e, 0x73, 0xec, 0x01, 0x00,
+	0x00,
 }

+ 1 - 2
api/pb/auth.proto

@@ -28,8 +28,7 @@ service AuthService {
 }
 
 message AuthStatusResponse {
-  bool is_authenticated = 1;
-  UserResponse.User user = 2;
+  UserResponse.User user = 1;
 }
 
 message AuthAuthenticateResponse {

+ 26 - 3
api/rest.go

@@ -3,6 +3,7 @@ package api
 import (
 	"fmt"
 	"net/http"
+	"strings"
 
 	"github.com/Sirupsen/logrus"
 	"github.com/asaskevich/govalidator"
@@ -24,7 +25,7 @@ func NewRESTServer(grpcPort string) (http.Handler, context.CancelFunc, error) {
 	}
 	endPoint := fmt.Sprintf("localhost:%s", grpcPort)
 	ctx = NewOriginTypeContext(ctx, OriginTypeREST)
-	gmux := runtime.NewServeMux()
+	gmux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true}))
 	opts := []grpc.DialOption{grpc.WithInsecure()}
 	err := pb.RegisterVPNServiceHandlerFromEndpoint(ctx, gmux, endPoint, opts)
 	if err != nil {
@@ -68,11 +69,11 @@ func NewRESTServer(grpcPort string) (http.Handler, context.CancelFunc, error) {
 		Path:     "auth",
 	}, mware)
 	mux.Handle("/", mware)
-	return mux, cancel, nil
+
+	return allowCORS(mux), cancel, nil
 }
 
 func specsHandler(w http.ResponseWriter, r *http.Request) {
-
 	w.Header().Set("Content-Type", "application/json")
 	switch r.URL.Path {
 	case "/specs/user.swagger.json":
@@ -102,3 +103,25 @@ func specsHandler(w http.ResponseWriter, r *http.Request) {
 		w.Write(vpnData)
 	}
 }
+
+func preflightHandler(w http.ResponseWriter, r *http.Request) {
+	headers := []string{"Content-Type", "Accept", "Authorization"}
+	w.Header().Set("Access-Control-Allow-Headers", strings.Join(headers, ","))
+	methods := []string{"GET", "HEAD", "POST", "PUT", "DELETE"}
+	w.Header().Set("Access-Control-Allow-Methods", strings.Join(methods, ","))
+	logrus.Debugf("rest: preflight request for %s", r.URL.Path)
+	return
+}
+
+func allowCORS(h http.Handler) http.Handler {
+	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+		if origin := r.Header.Get("Origin"); origin != "" {
+			w.Header().Set("Access-Control-Allow-Origin", origin)
+			if r.Method == "OPTIONS" && r.Header.Get("Access-Control-Request-Method") != "" {
+				preflightHandler(w, r)
+				return
+			}
+		}
+		h.ServeHTTP(w, r)
+	})
+}

Datei-Diff unterdrückt, da er zu groß ist
+ 0 - 0
bindata/bindata.go


+ 0 - 4
template/auth.swagger.json

@@ -113,10 +113,6 @@
     "pbAuthStatusResponse": {
       "type": "object",
       "properties": {
-        "is_authenticated": {
-          "type": "boolean",
-          "format": "boolean"
-        },
         "user": {
           "$ref": "#/definitions/UserResponseUser"
         }

Einige Dateien werden nicht angezeigt, da zu viele Dateien in diesem Diff geändert wurden.