-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ref DEV-1303
- Loading branch information
Showing
21 changed files
with
1,205 additions
and
28 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 |
---|---|---|
|
@@ -33,3 +33,6 @@ pkg/lib/webparcel/parcel_gen.go | |
|
||
# OSX | ||
.DS_Store | ||
|
||
# custom build of k6 | ||
/k6/k6 |
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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
rate_limits: | ||
disabled: true |
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,135 @@ | ||
package authgear | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/json" | ||
"encoding/pem" | ||
"strings" | ||
|
||
"github.com/google/uuid" | ||
"github.com/lestrrat-go/jwx/v2/jwa" | ||
"github.com/lestrrat-go/jwx/v2/jwk" | ||
"github.com/lestrrat-go/jwx/v2/jws" | ||
"go.k6.io/k6/js/modules" | ||
) | ||
|
||
func init() { | ||
modules.Register("k6/x/authgear", new(Authgear)) | ||
} | ||
|
||
func toJavaScript(v interface{}) interface{} { | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
var out interface{} | ||
err = json.Unmarshal(b, &out) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return out | ||
} | ||
|
||
func toJSONBytes(v interface{}) []byte { | ||
b, err := json.Marshal(v) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return b | ||
} | ||
|
||
type Authgear struct{} | ||
|
||
func (*Authgear) Uuid() string { | ||
return uuid.Must(uuid.NewRandom()).String() | ||
} | ||
|
||
func (*Authgear) GenerateRSAPrivateKeyInPKCS8PEM(bits int) string { | ||
key, err := rsa.GenerateKey(rand.Reader, bits) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
der, err := x509.MarshalPKCS8PrivateKey(key) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
block := &pem.Block{ | ||
Type: "PRIVATE KEY", | ||
Bytes: der, | ||
} | ||
var buf strings.Builder | ||
err = pem.Encode(&buf, block) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return buf.String() | ||
} | ||
|
||
func (*Authgear) JwkKeyFromPKCS8PEM(pkcs8Pem string) interface{} { | ||
key, err := jwk.ParseKey([]byte(pkcs8Pem), jwk.WithPEM(true)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return toJavaScript(key) | ||
} | ||
|
||
func (*Authgear) JwkPublicKeyFromJWKPrivateKey(jwkValue interface{}) interface{} { | ||
jwkBytes := toJSONBytes(jwkValue) | ||
jwkPrivateKey, err := jwk.ParseKey(jwkBytes) | ||
if err != nil { | ||
panic(err) | ||
} | ||
jwkPublicKey, err := jwkPrivateKey.PublicKey() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return toJavaScript(jwkPublicKey) | ||
} | ||
|
||
func (*Authgear) SignJWT(alg string, jwkValue interface{}, payload interface{}, headers map[string]interface{}) string { | ||
jwkBytes := toJSONBytes(jwkValue) | ||
jwkPrivateKey, err := jwk.ParseKey(jwkBytes) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
payloadBytes := toJSONBytes(payload) | ||
|
||
hdr := jws.NewHeaders() | ||
for k, v := range headers { | ||
switch k { | ||
case "jwk": | ||
jwkKey, err := jwk.ParseKey(toJSONBytes(v)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
err = hdr.Set(k, jwkKey) | ||
default: | ||
err = hdr.Set(k, v) | ||
} | ||
if err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
b, err := jws.Sign( | ||
payloadBytes, | ||
jws.WithKey( | ||
jwa.SignatureAlgorithm(alg), | ||
jwkPrivateKey, | ||
jws.WithProtectedHeaders(hdr), | ||
), | ||
) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
return string(b) | ||
} |
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,62 @@ | ||
authentication: | ||
identities: | ||
- login_id | ||
primary_authenticators: | ||
- password | ||
secondary_authentication_mode: disabled | ||
authenticator: | ||
oob_otp: | ||
sms: | ||
phone_otp_mode: sms | ||
http: | ||
public_origin: http://localhost:3100 | ||
id: loadtest | ||
identity: | ||
login_id: | ||
keys: | ||
- type: email | ||
localization: | ||
fallback_language: en | ||
supported_languages: | ||
- en | ||
oauth: | ||
clients: | ||
- access_token_lifetime_seconds: 300 | ||
client_id: test | ||
client_name: loadtest | ||
grant_types: | ||
- authorization_code | ||
- refresh_token | ||
issue_jwt_access_token: true | ||
name: loadtest | ||
redirect_uris: | ||
- "com.example://host" | ||
response_types: | ||
- none | ||
- code | ||
test_mode: | ||
oob_otp: | ||
enabled: true | ||
rules: | ||
- fixed_code: "000000" | ||
regex: .+ | ||
sms: | ||
enabled: true | ||
rules: | ||
- regex: .+ | ||
suppressed: true | ||
email: | ||
enabled: true | ||
rules: | ||
- regex: .+ | ||
suppressed: true | ||
ui: | ||
implementation: authflowv2 | ||
verification: | ||
claims: | ||
email: | ||
enabled: true | ||
required: false | ||
phone_number: | ||
enabled: true | ||
required: false |
Oops, something went wrong.