Golang Request.BasicAuth Examples

Golang Request.BasicAuth - 30 examples found. These are the top rated real world Golang examples of net/http.Request.BasicAuth extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
// AuthenticateRequest authenticates the request using the "Authorization: Basic" header in the request
func (a *Authenticator) AuthenticateRequest(req *http.Request) (user.Info, bool, error) {
	username, password, found := req.BasicAuth()
	if !found {
		return nil, false, nil
	}
	return a.auth.AuthenticatePassword(username, password)
}
Example #2
0
func (op *obtainTokenOp) parse(jp jsonParser, r *http.Request) error {
	v := mux.Vars(r)
	op.serverLocation = strings.ToLower(v["serverLocation"])

	// extract appID and appKey as BASIC auth.
	u, p, ok := r.BasicAuth()
	if !ok {
		return ErrNoAuthorization
	}
	if u == "" || p == "" {
		return ErrInvalidGrant
	}
	op.appID, op.appKey = u, p

	m, err := jp.parseJSON(r)
	if err != nil {
		return newInvalidUsernamePassword(err)
	}
	x := dproxy.New(m)
	op.username, err = x.M("username").String()
	if err != nil {
		return newInvalidUsernamePassword(err)
	}
	op.password, err = x.M("password").String()
	if err != nil {
		return newInvalidUsernamePassword(err)
	}
	return nil
}
Example #3
0
func (as *AuthServer) ParseRequest(req *http.Request) (*AuthRequest, error) {
	ar := &AuthRequest{RemoteAddr: req.RemoteAddr, Actions: []string{}}
	user, password, haveBasicAuth := req.BasicAuth()
	if haveBasicAuth {
		ar.User = user
		ar.Password = authn.PasswordString(password)
	}
	ar.Account = req.FormValue("account")
	if ar.Account == "" {
		ar.Account = ar.User
	} else if haveBasicAuth && ar.Account != ar.User {
		return nil, fmt.Errorf("user and account are not the same (%q vs %q)", ar.User, ar.Account)
	}
	ar.Service = req.FormValue("service")
	scope := req.FormValue("scope")
	if scope != "" {
		parts := strings.Split(scope, ":")
		if len(parts) != 3 {
			return nil, fmt.Errorf("invalid scope: %q", scope)
		}
		ar.Type = parts[0]
		ar.Name = parts[1]
		ar.Actions = strings.Split(parts[2], ",")
		sort.Strings(ar.Actions)
	}
	return ar, nil
}
Example #4
0
func (b *basicauth) ServeHTTP(w http.ResponseWriter, r *http.Request) {

	// don't request passwords for the websocket interface (for now)
	// because 'wscat' doesn't support that.
	if r.RequestURI == "/monitor" {
		b.h.ServeHTTP(w, r)
		return
	}

	cfgMutex.RLock()
	user := Config.HTTP.User
	password := Config.HTTP.Password
	cfgMutex.RUnlock()

	if len(user) == 0 {
		b.h.ServeHTTP(w, r)
		return
	}

	ruser, rpass, ok := r.BasicAuth()
	if ok {
		if ruser == user && rpass == password {
			b.h.ServeHTTP(w, r)
			return
		}
	}

	w.Header().Set("WWW-Authenticate", fmt.Sprintf(`Basic realm=%q`, "GeoDNS Status"))
	http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
	return
}
Example #5
0
// parseCredentials parses a request and returns the authentication credentials.
// The credentials may be present as URL query params, or as a Basic
// Authentication header.
// As params: http://127.0.0.1/query?u=username&p=password
// As basic auth: http://username:password@127.0.0.1
// As Bearer token in Authorization header: Bearer <JWT_TOKEN_BLOB>
func parseCredentials(r *http.Request) (*credentials, error) {
	q := r.URL.Query()

	// Check for the HTTP Authorization header.
	if s := r.Header.Get("Authorization"); s != "" {
		// Check for Bearer token.
		strs := strings.Split(s, " ")
		if len(strs) == 2 && strs[0] == "Bearer" {
			return &credentials{
				Method: BearerAuthentication,
				Token:  strs[1],
			}, nil
		}

		// Check for basic auth.
		if u, p, ok := r.BasicAuth(); ok {
			return &credentials{
				Method:   UserAuthentication,
				Username: u,
				Password: p,
			}, nil
		}
	}

	// Check for username and password in URL params.
	if u, p := q.Get("u"), q.Get("p"); u != "" && p != "" {
		return &credentials{
			Method:   UserAuthentication,
			Username: u,
			Password: p,
		}, nil
	}

	return nil, fmt.Errorf("unable to parse authentication credentials")
}
Example #6
0
func (hli *HttpListenInput) RequestHandler(w http.ResponseWriter, req *http.Request) {
	var err error

	if hli.conf.AuthType == "Basic" {
		if hli.conf.Username != "" && hli.conf.Password != "" {
			user, pass, ok := req.BasicAuth()
			if !ok || user != hli.conf.Username || pass != hli.conf.Password {
				err = fmt.Errorf("Basic Auth Failed")
				hli.ir.LogError(err)
			}
		}
	}
	if hli.conf.AuthType == "API" {
		if hli.conf.Key != "" {
			api_key := req.Header.Get("X-API-Key")
			if api_key != hli.conf.Key {
				err = fmt.Errorf("API Auth Failed")
				hli.ir.LogError(err)
			}
		}
	}
	if err == nil {
		sRunner := hli.ir.NewSplitterRunner(req.RemoteAddr)
		if !sRunner.UseMsgBytes() {
			sRunner.SetPackDecorator(hli.makePackDecorator(req))
		}
		err = sRunner.SplitStreamNullSplitterToEOF(req.Body, nil)
		if err != nil && err != io.EOF {
			hli.ir.LogError(fmt.Errorf("receiving request body: %s", err.Error()))
		}
		req.Body.Close()
		sRunner.Done()
	}
}