2020-03-07 11:06:57 +00:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
2020-03-17 20:04:16 +00:00
|
|
|
"fmt"
|
2020-03-07 11:06:57 +00:00
|
|
|
"net/http"
|
2020-03-17 20:04:16 +00:00
|
|
|
"net/url"
|
2020-03-07 11:06:57 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-04-02 17:44:48 +01:00
|
|
|
"github.com/micro/go-micro/v2/api/resolver"
|
2020-03-07 11:06:57 +00:00
|
|
|
"github.com/micro/go-micro/v2/auth"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CombinedAuthHandler wraps a server and authenticates requests
|
2020-04-02 17:44:48 +01:00
|
|
|
func CombinedAuthHandler(namespace string, r resolver.Resolver, h http.Handler) http.Handler {
|
2020-03-07 11:06:57 +00:00
|
|
|
return authHandler{
|
2020-04-02 17:44:48 +01:00
|
|
|
handler: h,
|
|
|
|
resolver: r,
|
|
|
|
auth: auth.DefaultAuth,
|
|
|
|
namespace: namespace,
|
2020-03-07 11:06:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type authHandler struct {
|
2020-04-02 17:44:48 +01:00
|
|
|
handler http.Handler
|
|
|
|
auth auth.Auth
|
|
|
|
resolver resolver.Resolver
|
|
|
|
namespace string
|
2020-03-07 11:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h authHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
2020-03-17 19:24:10 +00:00
|
|
|
// Extract the token from the request
|
|
|
|
var token string
|
|
|
|
if header := req.Header.Get("Authorization"); len(header) > 0 {
|
|
|
|
// Extract the auth token from the request
|
2020-03-25 11:20:53 +00:00
|
|
|
if strings.HasPrefix(header, auth.BearerScheme) {
|
|
|
|
token = header[len(auth.BearerScheme):]
|
2020-03-17 19:24:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Get the token out the cookies if not provided in headers
|
|
|
|
if c, err := req.Cookie("micro-token"); err == nil && c != nil {
|
2020-03-23 16:19:30 +00:00
|
|
|
token = strings.TrimPrefix(c.Value, auth.TokenCookieName+"=")
|
2020-03-25 11:20:53 +00:00
|
|
|
req.Header.Set("Authorization", auth.BearerScheme+token)
|
2020-03-17 19:24:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-07 11:06:57 +00:00
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// Get the account using the token, fallback to a blank account
|
|
|
|
// since some endpoints can be unauthenticated, so the lack of an
|
|
|
|
// account doesn't necesserially mean a forbidden request
|
|
|
|
acc, err := h.auth.Inspect(token)
|
|
|
|
if err != nil {
|
|
|
|
acc = &auth.Account{}
|
2020-03-07 11:06:57 +00:00
|
|
|
}
|
2020-04-02 17:44:48 +01:00
|
|
|
|
|
|
|
// Determine the name of the service being requested
|
|
|
|
endpoint, err := h.resolver.Resolve(req)
|
2020-04-03 09:18:30 +01:00
|
|
|
if err == resolver.ErrInvalidPath || err == resolver.ErrNotFound {
|
|
|
|
// a file not served by the resolver has been requested (e.g. favicon.ico)
|
|
|
|
endpoint = &resolver.Endpoint{Path: req.URL.Path}
|
|
|
|
} else if err != nil {
|
2020-04-02 17:44:48 +01:00
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2020-04-03 09:18:30 +01:00
|
|
|
|
|
|
|
// construct the resource name, e.g. home => go.micro.web.home
|
|
|
|
resName := h.namespace
|
|
|
|
if len(endpoint.Name) > 0 {
|
|
|
|
resName = resName + "." + endpoint.Name
|
|
|
|
}
|
2020-04-02 17:44:48 +01:00
|
|
|
|
|
|
|
// Perform the verification check to see if the account has access to
|
|
|
|
// the resource they're requesting
|
2020-03-23 16:19:30 +00:00
|
|
|
err = h.auth.Verify(acc, &auth.Resource{
|
|
|
|
Type: "service",
|
2020-04-02 17:44:48 +01:00
|
|
|
Name: resName,
|
|
|
|
Endpoint: endpoint.Path,
|
2020-03-23 16:19:30 +00:00
|
|
|
})
|
2020-03-17 16:03:49 +00:00
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// The account has the necessary permissions to access the
|
|
|
|
// resource
|
|
|
|
if err == nil {
|
|
|
|
h.handler.ServeHTTP(w, req)
|
|
|
|
return
|
2020-03-07 11:06:57 +00:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:19:30 +00:00
|
|
|
// The account is set, but they don't have enough permissions,
|
|
|
|
// hence we 403.
|
|
|
|
if len(acc.ID) > 0 {
|
|
|
|
w.WriteHeader(http.StatusForbidden)
|
2020-03-07 11:06:57 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is no auth login url set, 401
|
2020-03-23 16:19:30 +00:00
|
|
|
loginURL := h.auth.Options().LoginURL
|
2020-03-07 11:06:57 +00:00
|
|
|
if loginURL == "" {
|
2020-03-23 16:19:30 +00:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
2020-03-16 10:30:56 +00:00
|
|
|
return
|
2020-03-07 11:06:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Redirect to the login path
|
2020-03-17 20:04:16 +00:00
|
|
|
params := url.Values{"redirect_to": {req.URL.Path}}
|
|
|
|
loginWithRedirect := fmt.Sprintf("%v?%v", loginURL, params.Encode())
|
|
|
|
http.Redirect(w, req, loginWithRedirect, http.StatusTemporaryRedirect)
|
2020-03-07 11:06:57 +00:00
|
|
|
}
|