authenticator

package
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 29, 2022 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package authenticator package provides access to resources only to authenticated users

Index

Constants

This section is empty.

Variables

View Source
var IsAuthorizedRequest = func(next http.Handler) http.Handler {

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !initialized {
			next.ServeHTTP(w, r)
			return
		}
		notReqAuth := []string{
			"/api/v1/ping",
			"/api/v1/servicemgr/services",
			"/api/v1/servicemgr/services/notification/{serviceid}",
			"/api/v1/scoringmgr/score",
		}
		for _, url := range notReqAuth {

			if url == r.URL.Path {
				next.ServeHTTP(w, r)
				return
			}
		}

		if r.Header["Authorization"] != nil {

			token, err := jwt.Parse(r.Header["Authorization"][0], func(token *jwt.Token) (interface{}, error) {

				switch token.Header["alg"] {
				case "HS256":
					if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
						return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
					}
					return passphrase, nil
				case "RS256":
					if rsaKeyInitialized {
						if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
							return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
						}
						return verifyKey, nil
					}
					return nil, errors.New("RSA keys are not initialized")
				}
				return nil, errors.New("unsupported algo")
			})

			if err != nil {
				log.Error(logPrefix, err.Error())
			}

			if token.Valid {
				if claims, ok := token.Claims.(jwt.MapClaims); ok {
					name, _ := claims["aud"].(string)
					if err = authorizer.Authorizer(name, r); err == nil {
						next.ServeHTTP(w, r)
					}
				}
			}
		} else {
			log.Error(logPrefix, "Request doesn't contain an Authorization token")
		}
	})
}

IsAuthorizedRequest checks if the request is authorized

Functions

func Init

func Init(passPhraseJWTPath string)

Init sets the environments for securemgr

Types

type AuthenticationImpl added in v1.1.0

type AuthenticationImpl struct{}

AuthenticationImpl structure

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL