Skip to content

Commit

Permalink
all: Add more libs and format
Browse files Browse the repository at this point in the history
Signed-off-by: aeneasr <aeneas.rekkas@serlo.org>
  • Loading branch information
aeneasr committed Oct 23, 2018
1 parent 1da65fb commit d51436a
Show file tree
Hide file tree
Showing 23 changed files with 358 additions and 31 deletions.
1 change: 1 addition & 0 deletions .goimportsignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
vendor/
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
init:
go get -u github.com/sqs/goreturns

format:
goreturns -w -i -local github.com/ory .

test:
go test -race ./...
3 changes: 2 additions & 1 deletion cmdx/env_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package cmdx

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestEnvVarExamplesHelpMessage(t *testing.T) {
Expand Down
1 change: 0 additions & 1 deletion cmdx/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ package cmdx

import (
"fmt"

"os"

"github.com/spf13/cobra"
Expand Down
3 changes: 2 additions & 1 deletion corsx/corsx_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package corsx

import (
"github.com/stretchr/testify/assert"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHelpMessage(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions corsx/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import (
"net/http"
"strconv"

"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"

"github.com/ory/go-convenience/stringsx"
"github.com/rs/cors"
"github.com/spf13/viper"
)

func ParseOptions() cors.Options {
Expand Down
3 changes: 2 additions & 1 deletion dbal/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (
"time"

"github.com/jmoiron/sqlx"
"github.com/ory/x/sqlcon"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

"github.com/ory/x/sqlcon"
)

func Connect(db string, logger logrus.FieldLogger, memf func() error, sqlf func(db *sqlx.DB) error) error {
Expand Down
3 changes: 2 additions & 1 deletion flagx/flagx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package flagx
import (
"time"

"github.com/ory/x/cmdx"
"github.com/spf13/cobra"

"github.com/ory/x/cmdx"
)

func MustGetBool(cmd *cobra.Command, name string) bool {
Expand Down
1 change: 1 addition & 0 deletions healthx/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"net/http"

"github.com/julienschmidt/httprouter"

"github.com/ory/herodot"
)

Expand Down
3 changes: 2 additions & 1 deletion healthx/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ import (
"testing"

"github.com/julienschmidt/httprouter"
"github.com/ory/herodot"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/ory/herodot"
)

func TestHealth(t *testing.T) {
Expand Down
102 changes: 102 additions & 0 deletions josex/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*-
* Copyright 2014 Square Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package pkg

import (
"crypto/x509"
"encoding/pem"
"errors"
"fmt"

"gopkg.in/square/go-jose.v2"
)

func LoadJSONWebKey(json []byte, pub bool) (*jose.JSONWebKey, error) {
var jwk jose.JSONWebKey
err := jwk.UnmarshalJSON(json)
if err != nil {
return nil, err
}
if !jwk.Valid() {
return nil, errors.New("invalid JWK key")
}
if jwk.IsPublic() != pub {
return nil, errors.New("priv/pub JWK key mismatch")
}
return &jwk, nil
}

// LoadPublicKey loads a public key from PEM/DER/JWK-encoded data.
func LoadPublicKey(data []byte) (interface{}, error) {
input := data

block, _ := pem.Decode(data)
if block != nil {
input = block.Bytes
}

// Try to load SubjectPublicKeyInfo
pub, err0 := x509.ParsePKIXPublicKey(input)
if err0 == nil {
return pub, nil
}

cert, err1 := x509.ParseCertificate(input)
if err1 == nil {
return cert.PublicKey, nil
}

jwk, err2 := LoadJSONWebKey(data, true)
if err2 == nil {
return jwk, nil
}

return nil, fmt.Errorf("square/go-jose: parse error, got '%s', '%s' and '%s'", err0, err1, err2)
}

// LoadPrivateKey loads a private key from PEM/DER/JWK-encoded data.
func LoadPrivateKey(data []byte) (interface{}, error) {
input := data

block, _ := pem.Decode(data)
if block != nil {
input = block.Bytes
}

var priv interface{}
priv, err0 := x509.ParsePKCS1PrivateKey(input)
if err0 == nil {
return priv, nil
}

priv, err1 := x509.ParsePKCS8PrivateKey(input)
if err1 == nil {
return priv, nil
}

priv, err2 := x509.ParseECPrivateKey(input)
if err2 == nil {
return priv, nil
}

jwk, err3 := LoadJSONWebKey(input, false)
if err3 == nil {
return jwk, nil
}

return nil, fmt.Errorf("square/go-jose: parse error, got '%s', '%s', '%s' and '%s'", err0, err1, err2, err3)
}
3 changes: 2 additions & 1 deletion metricsx/example/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (
"time"

"github.com/julienschmidt/httprouter"
"github.com/ory/x/metricsx"
"github.com/segmentio/analytics-go"
"github.com/sirupsen/logrus"
"github.com/urfave/negroni"

"github.com/ory/x/metricsx"
)

func main() {
Expand Down
5 changes: 2 additions & 3 deletions metricsx/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ package metricsx
import (
"crypto/sha256"
"encoding/hex"
"io"
"math/rand"
"net"
"net/http"
"net/url"
Expand All @@ -31,9 +33,6 @@ import (
"sync"
"time"

"io"
"math/rand"

"github.com/pborman/uuid"
"github.com/segmentio/analytics-go"
"github.com/sirupsen/logrus"
Expand Down
54 changes: 54 additions & 0 deletions randx/sequence.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/

package sequence

import (
"crypto/rand"
"math/big"
)

var rander = rand.Reader // random function

var (
AlphaNum = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
Alpha = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
AlphaLowerNum = []rune("abcdefghijklmnopqrstuvwxyz0123456789")
AlphaUpperNum = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
AlphaLower = []rune("abcdefghijklmnopqrstuvwxyz")
AlphaUpper = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
Numeric = []rune("0123456789")
)

func RuneSequence(l int, allowedRunes []rune) (seq []rune, err error) {
c := big.NewInt(int64(len(allowedRunes)))
seq = make([]rune, l)

for i := 0; i < l; i++ {
r, err := rand.Int(rander, c)
if err != nil {
return seq, err
}
rn := allowedRunes[r.Uint64()]
seq[i] = rn
}

return seq, nil
}
102 changes: 102 additions & 0 deletions randx/sequence_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author Aeneas Rekkas <aeneas+oss@aeneas.io>
* @copyright 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
* @license Apache-2.0
*/

package sequence

import (
"regexp"
"testing"

"github.com/stretchr/testify/assert"
)

func TestRunePatterns(t *testing.T) {
for k, v := range []struct {
runes []rune
shouldMatch string
}{
{Alpha, "[a-zA-Z]{52}"},
{AlphaLower, "[a-z]{26}"},
{AlphaUpper, "[A-Z]{26}"},
{AlphaNum, "[a-zA-Z0-9]{62}"},
{AlphaLowerNum, "[a-z0-9]{36}"},
{AlphaUpperNum, "[A-Z0-9]{36}"},
{Numeric, "[0-9]{10}"},
} {
valid, err := regexp.Match(v.shouldMatch, []byte(string(v.runes)))
assert.Nil(t, err, "Case %d", k)
assert.True(t, valid, "Case %d", k)
}
}

func TestRuneSequenceMatchesPattern(t *testing.T) {
for k, v := range []struct {
runes []rune
shouldMatch string
length int
}{
{Alpha, "[a-zA-Z]+", 25},
{AlphaLower, "[a-z]+", 46},
{AlphaUpper, "[A-Z]+", 21},
{AlphaNum, "[a-zA-Z0-9]+", 123},
{AlphaLowerNum, "[a-z0-9]+", 41},
{AlphaUpperNum, "[A-Z0-9]+", 94914},
{Numeric, "[0-9]+", 94914},
} {
seq, err := RuneSequence(v.length, v.runes)
assert.Nil(t, err, "case %d", k)
assert.Equal(t, v.length, len(seq), "case %d", k)

valid, err := regexp.Match(v.shouldMatch, []byte(string(seq)))
assert.Nil(t, err, "case %d", k)
assert.True(t, valid, "case %d\nrunes %s\nresult %s", k, v.runes, string(seq))
}
}

func TestRuneSequenceIsPseudoUnique(t *testing.T) {
if testing.Short() {
t.SkipNow()
}

times := 100
runes := []rune("ab")
length := 32
s := make(map[string]bool)

for i := 0; i < times; i++ {
k, err := RuneSequence(length, runes)
assert.Nil(t, err)
ks := string(k)
_, ok := s[ks]
assert.False(t, ok)
if ok {
return
}
s[ks] = true
}
}

func BenchmarkTestInt64(b *testing.B) {
length := 25
pattern := []rune("abcdefghijklmnopqrstuvwxyz")
for i := 0; i < b.N; i++ {
RuneSequence(length, pattern)
}
}
Loading

0 comments on commit d51436a

Please sign in to comment.