|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + "net/http" |
| 6 | + "net/http/httputil" |
| 7 | + "net/url" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "encoding/base64" |
| 11 | + "encoding/json" |
| 12 | + |
| 13 | + "github.com/go-martini/martini" |
| 14 | + "github.com/martini-contrib/oauth2" |
| 15 | + "github.com/martini-contrib/sessions" |
| 16 | + "path/filepath" |
| 17 | +) |
| 18 | + |
| 19 | +type Server struct { |
| 20 | + Conf *Conf |
| 21 | +} |
| 22 | + |
| 23 | +type User struct { |
| 24 | + Email string |
| 25 | +} |
| 26 | + |
| 27 | +func NewServer(conf *Conf) *Server { |
| 28 | + return &Server{conf} |
| 29 | +} |
| 30 | + |
| 31 | +func (s *Server) Run() error { |
| 32 | + m := martini.Classic() |
| 33 | + |
| 34 | + m.Use(sessions.Sessions("session", sessions.NewCookieStore([]byte(s.Conf.Auth.Session.Key)))) |
| 35 | + m.Use(oauth2.Google(&oauth2.Options{ |
| 36 | + ClientId: s.Conf.Auth.Google.ClientId, |
| 37 | + ClientSecret: s.Conf.Auth.Google.ClientSecret, |
| 38 | + RedirectURL: s.Conf.Auth.Google.RedirectURL, |
| 39 | + Scopes: []string{"email"}, |
| 40 | + })) |
| 41 | + |
| 42 | + m.Use(oauth2.LoginRequired) |
| 43 | + m.Use(restrictDomain(s.Conf.Domain)) |
| 44 | + |
| 45 | + for i := range s.Conf.Proxies { |
| 46 | + p := s.Conf.Proxies[i] |
| 47 | + |
| 48 | + if strings.HasSuffix(p.Path, "/") == false { |
| 49 | + p.Path += "/" |
| 50 | + } |
| 51 | + strip_path := p.Path |
| 52 | + |
| 53 | + if strings.HasSuffix(p.Path, "**") == false { |
| 54 | + p.Path += "**" |
| 55 | + } |
| 56 | + |
| 57 | + u, err := url.Parse(p.Dest) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + |
| 62 | + proxy := httputil.NewSingleHostReverseProxy(u) |
| 63 | + if p.Strip { |
| 64 | + m.Any(p.Path, http.StripPrefix(strip_path, proxy)) |
| 65 | + } else { |
| 66 | + m.Any(p.Path, proxy) |
| 67 | + } |
| 68 | + |
| 69 | + log.Printf("register proxy path:%s dest:%s", strip_path, u.String()) |
| 70 | + } |
| 71 | + |
| 72 | + path, err := filepath.Abs(s.Conf.Htdocs) |
| 73 | + if err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + |
| 77 | + log.Printf("starting static file server for: %s", path) |
| 78 | + fileServer := http.FileServer(http.Dir(path)) |
| 79 | + m.Get("/**", fileServer.ServeHTTP) |
| 80 | + |
| 81 | + log.Printf("starting server at %s", s.Conf.Addr) |
| 82 | + |
| 83 | + if s.Conf.SSL.Cert != "" && s.Conf.SSL.Key != "" { |
| 84 | + return http.ListenAndServeTLS(s.Conf.Addr, s.Conf.SSL.Cert, s.Conf.SSL.Key, m) |
| 85 | + } else { |
| 86 | + return http.ListenAndServe(s.Conf.Addr, m) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +func forbidden(w http.ResponseWriter) { |
| 91 | + w.WriteHeader(403) |
| 92 | + w.Write([]byte("Access denied")) |
| 93 | +} |
| 94 | + |
| 95 | +// base64Decode decodes the Base64url encoded string |
| 96 | +// |
| 97 | +// steel from code.google.com/p/goauth2/oauth/jwt |
| 98 | +func base64Decode(s string) ([]byte, error) { |
| 99 | + // add back missing padding |
| 100 | + switch len(s) % 4 { |
| 101 | + case 2: |
| 102 | + s += "==" |
| 103 | + case 3: |
| 104 | + s += "=" |
| 105 | + } |
| 106 | + return base64.URLEncoding.DecodeString(s) |
| 107 | +} |
| 108 | + |
| 109 | +func restrictDomain(domain string) martini.Handler { |
| 110 | + return func(c martini.Context, tokens oauth2.Tokens, w http.ResponseWriter) { |
| 111 | + extra := tokens.ExtraData() |
| 112 | + if _, ok := extra["id_token"]; ok == false { |
| 113 | + log.Printf("id_token not found") |
| 114 | + forbidden(w) |
| 115 | + return |
| 116 | + } |
| 117 | + |
| 118 | + keys := strings.Split(extra["id_token"], ".") |
| 119 | + if len(keys) < 2 { |
| 120 | + log.Printf("invalid id_token") |
| 121 | + forbidden(w) |
| 122 | + return |
| 123 | + } |
| 124 | + |
| 125 | + data, err := base64Decode(keys[1]) |
| 126 | + if err != nil { |
| 127 | + log.Printf("failed to decode base64: %s", err.Error()) |
| 128 | + forbidden(w) |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + var info map[string]interface{} |
| 133 | + if err := json.Unmarshal(data, &info); err != nil { |
| 134 | + log.Printf("failed to decode json: %s", err.Error()) |
| 135 | + forbidden(w) |
| 136 | + return |
| 137 | + } |
| 138 | + |
| 139 | + if email, ok := info["email"].(string); ok { |
| 140 | + if domain == "" || strings.HasSuffix(email, "@"+domain) { |
| 141 | + user := &User{email} |
| 142 | + c.Map(user) |
| 143 | + } else { |
| 144 | + log.Printf("email doesn't allow: %s", email) |
| 145 | + forbidden(w) |
| 146 | + return |
| 147 | + } |
| 148 | + } else { |
| 149 | + log.Printf("email not found") |
| 150 | + forbidden(w) |
| 151 | + return |
| 152 | + } |
| 153 | + } |
| 154 | +} |
0 commit comments