<commit_before>package backend

import (
	"errors"
	"time"

	"github.com/aukbit/pluto"
	"github.com/aukbit/pluto/auth/jwt"
	pba "github.com/aukbit/pluto/auth/proto"
	"github.com/aukbit/pluto/client"
	pbu "github.com/aukbit/pluto/examples/user/proto"
	"golang.org/x/net/context"
)

var (
	privKeyPath = "./keys/auth.rsa"
	pubKeyPath  = "./keys/auth.rsa.pub"
)

var (
	errCredentials            = errors.New("Invalid credentials")
	errClientUserNotAvailable = errors.New("Client user not available")
)

// AuthViews struct
type AuthViews struct{}

// Authenticate implements authentication
func (av *AuthViews) Authenticate(ctx context.Context, cre *pba.Credentials) (*pba.Token, error) {
	// get client user from pluto service from context
	c, ok := ctx.Value(pluto.Key("pluto")).(*pluto.Service).Client("user")
	if !ok {
		return &pba.Token{}, errClientUserNotAvailable
	}
	// dial
	conn, err := c.Dial(client.Timeout(5 * time.Second))
	if err != nil {
		return &pba.Token{}, err
	}
	defer conn.Close()
	// make a call to user backend service for credentials verification
	nCred := &pbu.Credentials{Email: cre.Email, Password: <PASSWORD>.Password}
	v, err := c.Stub(conn).(pbu.UserServiceClient).VerifyUser(ctx, nCred)
	if err != nil {
		return &pba.Token{}, err
	}
	if !v.IsValid {
		return &pba.Token{}, errCredentials
	}
	pk, err := jwt.LoadPrivateKey(privKeyPath)
	if err != nil {
		return &pba.Token{}, err
	}
	token, err := jwt.NewToken(cre.Email, "bearer", "", 3650, pk)
	if err != nil {
		return &pba.Token{}, err
	}
	return &pba.Token{Jwt: token}, nil
}

// Verify implements authentication
func (av *AuthViews) Verify(ctx context.Context, t *pba.Token) (*pba.Verification, error) {
	pk, err := jwt.LoadPublicKey(pubKeyPath)
	if err != nil {
		return &pba.Verification{IsValid: false}, err
	}
	err = jwt.Verify(t.Jwt, pk)
	if err != nil {
		return &pba.Verification{IsValid: false}, err
	}
	return &pba.Verification{IsValid: true}, nil
}<commit_msg>fix error jwt backend example<commit_after>package backend

import (
	"errors"
	"time"

	"github.com/aukbit/pluto"
	"github.com/aukbit/pluto/auth/jwt"
	pba "github.com/aukbit/pluto/auth/proto"
	"github.com/aukbit/pluto/client"
	pbu "github.com/aukbit/pluto/examples/user/proto"
	"golang.org/x/net/context"
)

var (
	privKeyPath = "./keys/auth.rsa"
	pubKeyPath  = "./keys/auth.rsa.pub"
)

var (
	errCredentials            = errors.New("Invalid credentials")
	errClientUserNotAvailable = errors.New("Client user not available")
)

// AuthViews struct
type AuthViews struct{}

// Authenticate implements authentication
func (av *AuthViews) Authenticate(ctx context.Context, cre *pba.Credentials) (*pba.Token, error) {
	// get client user from pluto service from context
	c, ok := ctx.Value(pluto.Key("pluto")).(*pluto.Service).Client("user")
	if !ok {
		return &pba.Token{}, errClientUserNotAvailable
	}
	// dial
	conn, err := c.Dial(client.Timeout(5 * time.Second))
	if err != nil {
		return &pba.Token{}, err
	}
	defer conn.Close()
	// make a call to user backend service for credentials verification
	nCred := &pbu.Credentials{Email: cre.Email, Password: cre.Password}
	v, err := c.Stub(conn).(pbu.UserServiceClient).VerifyUser(ctx, nCred)
	if err != nil {
		return &pba.Token{}, err
	}
	if !v.IsValid {
		return &pba.Token{}, errCredentials
	}
	pk, err := jwt.LoadPrivateKey(privKeyPath)
	if err != nil {
		return &pba.Token{}, err
	}
	cs := &jwt.ClaimSet{
		Identifier: cre.Email,
		Audience:   "bearer",
		Expiration: 3650,
	}
	token, err := jwt.NewToken(cs, pk)
	if err != nil {
		return &pba.Token{}, err
	}
	return &pba.Token{Jwt: token}, nil
}

// Verify implements authentication
func (av *AuthViews) Verify(ctx context.Context, t *pba.Token) (*pba.Verification, error) {
	pk, err := jwt.LoadPublicKey(pubKeyPath)
	if err != nil {
		return &pba.Verification{IsValid: false}, err
	}
	err = jwt.Verify(t.Jwt, pk)
	if err != nil {
		return &pba.Verification{IsValid: false}, err
	}
	return &pba.Verification{IsValid: true}, nil
}
{'fraction_non_alphanumeric': 0.15405811623246493, 'fraction_numerical': 0.00250501002004008, 'mean_word_length': 4.086624203821656, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7469629', 'n_tokens_mistral': 1517, 'n_tokens_neox': 1498, 'n_words': 338}