Skip to content

Commit

Permalink
export http authenticator (go-openapi#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
casualjim authored Jan 8, 2017
1 parent 75dade3 commit 6c55ee7
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 7 deletions.
26 changes: 26 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# top-most EditorConfig file
root = true

# Unix-style newlines with a newline ending every file
[*]
end_of_line = lf
insert_final_newline = true
indent_style = space
indent_size = 2
trim_trailing_whitespace = true

# Set default charset
[*.{js,py,go,scala,rb,java,html,css,less,sass,md}]
charset = utf-8

# Tab indentation (no size specified)
[*.go]
indent_style = tab

[*.md]
trim_trailing_whitespace = false

# Matches the exact files either package.json or .travis.yml
[{package.json,.travis.yml}]
indent_style = space
indent_size = 2
13 changes: 7 additions & 6 deletions security/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"github.com/go-openapi/runtime"
)

// httpAuthenticator is a function that authenticates a HTTP request
func httpAuthenticator(handler func(*http.Request) (bool, interface{}, error)) runtime.Authenticator {
// HttpAuthenticator is a function that authenticates a HTTP request
func HttpAuthenticator(handler func(*http.Request) (bool, interface{}, error)) runtime.Authenticator {
return runtime.AuthenticatorFunc(func(params interface{}) (bool, interface{}, error) {
if request, ok := params.(*http.Request); ok {
return handler(request)
Expand All @@ -35,7 +35,8 @@ func httpAuthenticator(handler func(*http.Request) (bool, interface{}, error)) r
})
}

func scopedAuthenticator(handler func(*ScopedAuthRequest) (bool, interface{}, error)) runtime.Authenticator {
// ScopedAuthenticator is a function that authenticates a HTTP request against a list of valid scopes
func ScopedAuthenticator(handler func(*ScopedAuthRequest) (bool, interface{}, error)) runtime.Authenticator {
return runtime.AuthenticatorFunc(func(params interface{}) (bool, interface{}, error) {
if request, ok := params.(*ScopedAuthRequest); ok {
return handler(request)
Expand All @@ -55,7 +56,7 @@ type ScopedTokenAuthentication func(string, []string) (interface{}, error)

// BasicAuth creates a basic auth authenticator with the provided authentication function
func BasicAuth(authenticate UserPassAuthentication) runtime.Authenticator {
return httpAuthenticator(func(r *http.Request) (bool, interface{}, error) {
return HttpAuthenticator(func(r *http.Request) (bool, interface{}, error) {
if usr, pass, ok := r.BasicAuth(); ok {
p, err := authenticate(usr, pass)
return true, p, err
Expand All @@ -82,7 +83,7 @@ func APIKeyAuth(name, in string, authenticate TokenAuthentication) runtime.Authe
getToken = func(r *http.Request) string { return r.URL.Query().Get(name) }
}

return httpAuthenticator(func(r *http.Request) (bool, interface{}, error) {
return HttpAuthenticator(func(r *http.Request) (bool, interface{}, error) {
token := getToken(r)
if token == "" {
return false, nil, nil
Expand All @@ -102,7 +103,7 @@ type ScopedAuthRequest struct {
// BearerAuth for use with oauth2 flows
func BearerAuth(name string, authenticate ScopedTokenAuthentication) runtime.Authenticator {
const prefix = "Bearer "
return scopedAuthenticator(func(r *ScopedAuthRequest) (bool, interface{}, error) {
return ScopedAuthenticator(func(r *ScopedAuthRequest) (bool, interface{}, error) {
var token string
hdr := r.Request.Header.Get("Authorization")
if strings.HasPrefix(hdr, prefix) {
Expand Down
2 changes: 1 addition & 1 deletion statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package runtime

// Statuses lists the most common HTTP status codes to default message
// taken from http://status.es
// taken from https://httpstatuses.com/
var Statuses = map[int]string{
100: "Continue",
101: "Switching Protocols",
Expand Down

0 comments on commit 6c55ee7

Please sign in to comment.