Skip to content

Commit

Permalink
doc doc doc
Browse files Browse the repository at this point in the history
  • Loading branch information
leberKleber committed Oct 23, 2020
1 parent 8e9b74d commit 0372a52
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 18 deletions.
25 changes: 23 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,23 @@ User specific custom-claims also available for jwt-generation and mail rendering

dockerized: https://hub.docker.com/r/leberkleber/simple-jwt-provider

# Table of contents
- [Try it](#try-it)
- [Getting started](#getting-started)
- [Generate ECDSA-512 key pair](#generate-ecdsa-512-key-pair)
- [Configuration](#configuration)
- [API](#api)
- [POST `/v1/auth/login`](#post-v1authlogin)
- [POST `/v1/auth/password-reset-request`](#post-v1authpassword-reset-request)
- [POST `/v1/auth/password-reset`](#post-v1authpassword-reset)
- [POST `/v1/admin/users`](#post-v1adminusers)
- [PUT `/v1/admin/users/{email}`](#put-v1adminusersemail)
- [DELETE `/v1/admin/users/{email}`](#delete-v1adminusersemail)
- [Development](#development)
- [mocks](#mocks)

## Try it
```bash
```shell script
git clone git@github.com:leberKleber/simple-jwt-provider.git
docker-compose -f example/docker-compose.yml up

Expand All @@ -36,7 +51,6 @@ docker-compose -f example/docker-compose.yml up

# 3) do crud operations on user
# see ./example/*.sh

```

## Getting started
Expand Down Expand Up @@ -166,3 +180,10 @@ This endpoint will delete the user with the given email when there are no tokens

Response body (201 - NO CONTENT)

## Development
### mocks
Mocks will be generated with github.com/matryer/moq. Execute the following for generation:
```shell script
go get github.com/matryer/moq
go generate ./...
```
6 changes: 2 additions & 4 deletions internal/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ type User struct {
Claims map[string]interface{}
}

/**
Creates new user with given email, password and claims.
return ErrUserAlreadyExists when user already exists
*/
// CreateUser creates new user with given email, password and claims.
// return ErrUserAlreadyExists when user already exists
func (p Provider) CreateUser(user User) error {
securedPassword, err := bcryptPassword(user.Password)
if err != nil {
Expand Down
18 changes: 6 additions & 12 deletions internal/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@ var ErrUserNotFound = errors.New("user not found")
var ErrNoValidTokenFound = errors.New("no valid token found")
var nowFunc = time.Now

/**
Check email / password combination and return a new jwt if correct.
return ErrIncorrectPassword when password is incorrect
return UserNotFoundErr when user not found
*/
// Login checks email / password combination and return a new jwt if correct.
// return ErrIncorrectPassword when password is incorrect
// return ErrUserNotFound when user not found
func (p Provider) Login(email, password string) (string, error) {
u, err := p.Storage.User(email)
if err != nil {
Expand All @@ -36,10 +34,8 @@ func (p Provider) Login(email, password string) (string, error) {
return p.JWTGenerator.Generate(email, u.Claims)
}

/**
CreatePasswordResetRequest send a password-reset-request email to the give address.
return ErrUserNotFound when user does not exists
*/
// CreatePasswordResetRequest send a password-reset-request email to the give address.
// return ErrUserNotFound when user does not exists
func (p Provider) CreatePasswordResetRequest(email string) error {
u, err := p.Storage.User(email)
if err != nil {
Expand Down Expand Up @@ -72,9 +68,7 @@ func (p Provider) CreatePasswordResetRequest(email string) error {
return nil
}

/**
ResetPassword resets the password of the given account if the reset token is correct.
*/
// ResetPassword resets the password of the given account if the reset token is correct.
func (p *Provider) ResetPassword(email, resetToken, newPassword string) error {
tokens, err := p.Storage.TokensByEMailAndToken(email, resetToken)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions internal/jwt/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ func NewGenerator(privateKey, jwtAudience, jwtIssuer, jwtSubject string) (*Gener
}, err
}

// Generate generates a valid jwt based on the Generator.privateKey. The jwt is issued to the given email and enriched
// with the given claims.
// 'userClaims' can be contain all json compatible types
func (g Generator) Generate(email string, userClaims map[string]interface{}) (string, error) {
now := nowFunc()
jwtID, err := uuid.NewRandom()
Expand Down
2 changes: 2 additions & 0 deletions internal/storage/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func New(dbHost string, dbPort int, dbUsername, dbPassword, dbName string) (*Sto
}, nil
}

// Migrate executes all sql migration files from the configures db-migrations folder. Should always be called before
// start
func (s Storage) Migrate(dbMigrationsPath string) error {
driver, err := postgres.WithInstance(s.db, &postgres.Config{})
if err != nil {
Expand Down

0 comments on commit 0372a52

Please sign in to comment.