Skip to content
This repository has been archived by the owner on Feb 8, 2023. It is now read-only.

Commit

Permalink
move crypto to a individual library: teambition/crypto-go
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Mar 16, 2017
1 parent 2ccd376 commit a9469b5
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 543 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Expand Up @@ -8,9 +8,7 @@ before_install:
- go get github.com/mattn/goveralls
script:
- go test -coverprofile=auth.coverprofile
- go test -coverprofile=crypto.coverprofile ./crypto
- go test -coverprofile=jwt.coverprofile ./jwt
- go test -coverprofile=pbkdf2.coverprofile ./pbkdf2
- gover
- go tool cover -html=gover.coverprofile
- goveralls -coverprofile=gover.coverprofile -service=travis-ci
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2016 Teambition
Copyright (c) 2016-2017 Teambition

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
4 changes: 0 additions & 4 deletions Makefile
@@ -1,15 +1,11 @@
test:
go test --race
go test --race ./crypto
go test --race ./jwt
go test --race ./pbkdf2

cover:
rm -f *.coverprofile
go test -coverprofile=auth.coverprofile
go test -coverprofile=crypto.coverprofile ./crypto
go test -coverprofile=jwt.coverprofile ./jwt
go test -coverprofile=pbkdf2.coverprofile ./pbkdf2
gover
go tool cover -html=gover.coverprofile
rm -f *.coverprofile
Expand Down
100 changes: 53 additions & 47 deletions README.md
@@ -1,16 +1,20 @@
Gear-Auth
====
Auth library with some useful JWT and Crypto methods.
# Gear-Auth

Auth library base on JWT.

[![Build Status](http://img.shields.io/travis/teambition/gear-auth.svg?style=flat-square)](https://travis-ci.org/teambition/gear-auth)
[![Coverage Status](http://img.shields.io/coveralls/teambition/gear-auth.svg?style=flat-square)](https://coveralls.io/r/teambition/gear-auth)
[![License](http://img.shields.io/badge/license-mit-blue.svg?style=flat-square)](https://raw.githubusercontent.com/teambition/gear-auth/master/LICENSE)
[![GoDoc](http://img.shields.io/badge/go-documentation-blue.svg?style=flat-square)](http://godoc.org/github.com/teambition/gear-auth)

## Crypto Library

https://github.com/teambition/crypto-go

## Demo

### Create a token and verify it.

```go
auther := auth.New([]byte("key1"))
token, _ := auther.JWT().Sign(jwt.Claims{"test": "OK"})
Expand All @@ -20,59 +24,60 @@ fmt.Println(claims.Get("test"))
```

### Use with Gear.

```go
package main

import (
"fmt"
"io/ioutil"
"net/http"

"github.com/SermoDigital/jose/jwt"
"github.com/mozillazg/request"
"github.com/teambition/gear"
"github.com/teambition/gear-auth"
"fmt"
"io/ioutil"
"net/http"

"github.com/SermoDigital/jose/jwt"
"github.com/mozillazg/request"
"github.com/teambition/gear"
"github.com/teambition/gear-auth"
)

func NewRequst() *request.Request {
c := &http.Client{}
return request.NewRequest(c)
c := &http.Client{}
return request.NewRequest(c)
}

func main() {
auther := auth.New([]byte("some_key")))
auther.JWT().SetIssuer("Gear")
// auther.JWT().SetExpiration(time.Hour * 24)

app := gear.New()

// use auther as middleware, if authentication failure, next middleware will not run.
app.UseHandler(auther)

app.Use(func(ctx *gear.Context) error {
claims, err := auther.FromCtx(ctx)
if err != nil {
return err // means Authentication failure.
}
return ctx.JSON(200, claims)
})
srv := app.Start()
defer srv.Close()

req := NewRequst()
host := "http://" + srv.Addr().String()

// create a token
claims := jwt.Claims{}
claims.Set("Hello", "world")
token, _ := auther.JWT().Sign(claims)
req.Headers["Authorization"] = "BEARER " + token
res, _ := req.Get(host)
defer res.Body.Close()

body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
// Output: {"Hello":"world","iss":"Gear"}
auther := auth.New([]byte("some_key")))
auther.JWT().SetIssuer("Gear")
// auther.JWT().SetExpiration(time.Hour * 24)

app := gear.New()

// use auther as middleware, if authentication failure, next middleware will not run.
app.UseHandler(auther)

app.Use(func(ctx *gear.Context) error {
claims, err := auther.FromCtx(ctx)
if err != nil {
return err // means Authentication failure.
}
return ctx.JSON(200, claims)
})
srv := app.Start()
defer srv.Close()

req := NewRequst()
host := "http://" + srv.Addr().String()

// create a token
claims := jwt.Claims{}
claims.Set("Hello", "world")
token, _ := auther.JWT().Sign(claims)
req.Headers["Authorization"] = "BEARER " + token
res, _ := req.Get(host)
defer res.Body.Close()

body, _ := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
// Output: {"Hello":"world","iss":"Gear"}
}
```

Expand All @@ -81,5 +86,6 @@ func main() {
https://godoc.org/github.com/teambition/gear-auth

## License

Gear-Auth is licensed under the [MIT](https://github.com/teambition/gear-auth/blob/master/LICENSE) license.
Copyright © 2016 [Teambition](https://www.teambition.com).
Copyright © 2016-2017 [Teambition](https://www.teambition.com).
14 changes: 1 addition & 13 deletions auth.go
Expand Up @@ -5,12 +5,11 @@ import (

josejwt "github.com/SermoDigital/jose/jwt"
"github.com/teambition/gear"
"github.com/teambition/gear-auth/crypto"
"github.com/teambition/gear-auth/jwt"
)

// Version ...
const Version = "1.4.4"
const Version = "1.5.0"

// TokenExtractor is a function that takes a gear.Context as input and
// returns either a string token or an empty string. Default to:
Expand All @@ -29,7 +28,6 @@ type TokenExtractor func(ctx *gear.Context) (token string)
// Auth is helper type. It combine JWT and Crypto object, and some useful mothod for JWT.
// You can use it as a gear middleware.
type Auth struct {
c *crypto.Crypto
j *jwt.JWT
ex TokenExtractor
}
Expand All @@ -49,21 +47,11 @@ func New(keys ...interface{}) *Auth {
return a
}

// Crypto returns internal Crypto instance. Default to nil, you should set it with SetCrypto
func (a *Auth) Crypto() *crypto.Crypto {
return a.c
}

// JWT returns internal JWT instance.
func (a *Auth) JWT() *jwt.JWT {
return a.j
}

// SetCrypto sets a Crypto instance to auth.
func (a *Auth) SetCrypto(c *crypto.Crypto) {
a.c = c
}

// SetJWT set a JWT instance to auth.
func (a *Auth) SetJWT(j *jwt.JWT) {
a.j = j
Expand Down
11 changes: 0 additions & 11 deletions auth_test.go
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/mozillazg/request"
"github.com/stretchr/testify/assert"
"github.com/teambition/gear"
"github.com/teambition/gear-auth/crypto"
)

func NewRequst() *request.Request {
Expand Down Expand Up @@ -138,14 +137,4 @@ func TestGearAuth(t *testing.T) {
assert.Equal(401, res.StatusCode)
res.Body.Close()
})

t.Run("should work with Crypto", func(t *testing.T) {
assert := assert.New(t)

a := New([]byte("my key 1"))
assert.Nil(a.Crypto())
c := crypto.New([]byte("my key 1"))
a.SetCrypto(c)
assert.Equal(c, a.Crypto())
})
}
145 changes: 0 additions & 145 deletions crypto/crypto.go

This file was deleted.

0 comments on commit a9469b5

Please sign in to comment.