-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: aeneasr <aeneas.rekkas@serlo.org>
- Loading branch information
aeneasr
committed
Oct 23, 2018
1 parent
1da65fb
commit d51436a
Showing
23 changed files
with
358 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
vendor/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,6 @@ package cmdx | |
|
||
import ( | ||
"fmt" | ||
|
||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ import ( | |
"net/http" | ||
|
||
"github.com/julienschmidt/httprouter" | ||
|
||
"github.com/ory/herodot" | ||
) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.