snapshot of https://github.com/ory/kratos.git (master, 2026-09-15; blobs > 131072B omitted)
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
Some checks failed
CI / Run tests and lints (push) Failing after 29s
CI / Build CLI docs (push) Has been skipped
CI / Run Playwright end-to-end tests (mysql) (push) Failing after 3s
CI / Run Playwright end-to-end tests (postgres) (push) Failing after 2s
CI / Run Playwright end-to-end tests (sqlite) (push) Failing after 3s
CodeQL / Analyze (go) (push) Failing after 4s
CodeQL / Analyze (javascript) (push) Failing after 3s
Docker Image Scanners / scanners (push) Failing after 4s
Synchronize Issue Labels / Synchronize Issue Labels (push) Has been skipped
CI / Run end-to-end tests (postgres) (push) Failing after 19s
CI / Run end-to-end tests (sqlite) (push) Failing after 2s
CI / Run Playwright end-to-end tests (cockroach) (push) Failing after 2s
CI / Draft newsletter (push) Has been skipped
CI / Generate release (push) Has been skipped
CI / Pending approval Slack notification (push) Has been skipped
CI / Send newsletter (push) Has been skipped
This commit is contained in:
36
examples/go/identity/create/main.go
Normal file
36
examples/go/identity/create/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
"github.com/ory/kratos/x"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func createIdentity() *ory.Identity {
|
||||
ctx := context.Background()
|
||||
|
||||
identity, res, err := client.IdentityAPI.CreateIdentity(ctx).
|
||||
CreateIdentityBody(ory.CreateIdentityBody{
|
||||
SchemaId: "default",
|
||||
Traits: map[string]interface{}{
|
||||
"email": "dev+" + x.NewUUID().String() + "@ory.sh",
|
||||
},
|
||||
}).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return identity
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(createIdentity())
|
||||
}
|
||||
17
examples/go/identity/create/main_test.go
Normal file
17
examples/go/identity/create/main_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
client = pkg.TestClient(t)
|
||||
require.NotEmpty(t, createIdentity().Id)
|
||||
}
|
||||
30
examples/go/identity/delete/main.go
Normal file
30
examples/go/identity/delete/main.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func deleteIdentity() {
|
||||
ctx := context.Background()
|
||||
|
||||
identity := pkg.CreateIdentity(client)
|
||||
|
||||
res, err := client.IdentityAPI.DeleteIdentity(ctx, identity.Id).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
deleteIdentity()
|
||||
fmt.Println("Success")
|
||||
}
|
||||
18
examples/go/identity/delete/main_test.go
Normal file
18
examples/go/identity/delete/main_test.go
Normal file
@@ -0,0 +1,18 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
deleteIdentity()
|
||||
}
|
||||
30
examples/go/identity/get/main.go
Normal file
30
examples/go/identity/get/main.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func getIdentity() *ory.Identity {
|
||||
ctx := context.Background()
|
||||
created := pkg.CreateIdentity(client)
|
||||
|
||||
identity, res, err := client.IdentityAPI.GetIdentity(ctx, created.Id).IncludeCredential([]string{"password"}).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return identity
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(getIdentity())
|
||||
}
|
||||
17
examples/go/identity/get/main_test.go
Normal file
17
examples/go/identity/get/main_test.go
Normal file
@@ -0,0 +1,17 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
client = pkg.TestClient(t)
|
||||
require.NotEmpty(t, getIdentity().Id)
|
||||
}
|
||||
35
examples/go/identity/update/main.go
Normal file
35
examples/go/identity/update/main.go
Normal file
@@ -0,0 +1,35 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
"github.com/ory/kratos/x"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func updateIdentity() *ory.Identity {
|
||||
ctx := context.Background()
|
||||
toUpdate := pkg.CreateIdentity(client)
|
||||
|
||||
identity, res, err := client.IdentityAPI.UpdateIdentity(ctx, toUpdate.Id).UpdateIdentityBody(ory.UpdateIdentityBody{
|
||||
Traits: map[string]interface{}{
|
||||
"email": "dev+not-" + x.NewUUID().String() + "@ory.sh",
|
||||
},
|
||||
}).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return identity
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(updateIdentity())
|
||||
}
|
||||
23
examples/go/identity/update/main_test.go
Normal file
23
examples/go/identity/update/main_test.go
Normal file
@@ -0,0 +1,23 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
identity := updateIdentity()
|
||||
require.NotEmpty(t, identity.Id)
|
||||
assert.Contains(t, identity.Traits.(map[string]interface{})["email"].(string), "dev+not")
|
||||
}
|
||||
61
examples/go/pkg/common.go
Normal file
61
examples/go/pkg/common.go
Normal file
@@ -0,0 +1,61 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/cookiejar"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
"github.com/ory/kratos/x"
|
||||
)
|
||||
|
||||
func PrintJSONPretty(v interface{}) {
|
||||
out, _ := json.MarshalIndent(v, "", " ")
|
||||
fmt.Println(string(out))
|
||||
}
|
||||
|
||||
func TestClient(t *testing.T) *ory.APIClient {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
return NewSDKForSelfHosted(publicURL)
|
||||
}
|
||||
|
||||
func NewSDK(project string) *ory.APIClient {
|
||||
return NewSDKForSelfHosted(fmt.Sprintf("https://%s.projects.oryapis.com/api/kratos/public", project))
|
||||
}
|
||||
|
||||
func NewSDKForSelfHosted(endpoint string) *ory.APIClient {
|
||||
conf := ory.NewConfiguration()
|
||||
conf.Servers = ory.ServerConfigurations{{URL: endpoint}}
|
||||
cj, _ := cookiejar.New(nil)
|
||||
conf.HTTPClient = &http.Client{Jar: cj}
|
||||
return ory.NewAPIClient(conf)
|
||||
}
|
||||
|
||||
func ExitOnError(err error) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
out, _ := json.MarshalIndent(err, "", " ")
|
||||
fmt.Printf("%s\n\nAn error occurred: %+v\n", out, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func SDKExitOnError(err error, res *http.Response) {
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
var body []byte
|
||||
if res != nil {
|
||||
body, _ = json.MarshalIndent(json.RawMessage(x.MustReadAll(res.Body)), "", " ")
|
||||
}
|
||||
out, _ := json.MarshalIndent(err, "", " ")
|
||||
fmt.Printf("%s\n\nAn error occurred: %+v\nbody: %s\n", out, err, body)
|
||||
os.Exit(1)
|
||||
}
|
||||
66
examples/go/pkg/resources.go
Normal file
66
examples/go/pkg/resources.go
Normal file
@@ -0,0 +1,66 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package pkg
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
func RandomCredentials() (email, password string) {
|
||||
email = "dev+" + uuid.Must(uuid.NewV4()).String() + "@ory.sh"
|
||||
password = strings.ReplaceAll(uuid.Must(uuid.NewV4()).String(), "-", "")
|
||||
return
|
||||
}
|
||||
|
||||
// CreateIdentityWithSession creates an identity and an Ory Session Token for it.
|
||||
func CreateIdentityWithSession(c *ory.APIClient, email, password string) (*ory.Session, string) {
|
||||
ctx := context.Background()
|
||||
|
||||
if email == "" {
|
||||
email, _ = RandomCredentials()
|
||||
}
|
||||
|
||||
if password == "" {
|
||||
_, password = RandomCredentials()
|
||||
}
|
||||
|
||||
// Initialize a registration flow
|
||||
flow, _, err := c.FrontendAPI.CreateNativeRegistrationFlow(ctx).Execute()
|
||||
ExitOnError(err)
|
||||
|
||||
// Submit the registration flow
|
||||
result, res, err := c.FrontendAPI.UpdateRegistrationFlow(ctx).Flow(flow.Id).UpdateRegistrationFlowBody(
|
||||
ory.UpdateRegistrationFlowWithPasswordMethodAsUpdateRegistrationFlowBody(&ory.UpdateRegistrationFlowWithPasswordMethod{
|
||||
Method: "password",
|
||||
Password: password,
|
||||
Traits: map[string]interface{}{"email": email},
|
||||
}),
|
||||
).Execute()
|
||||
SDKExitOnError(err, res)
|
||||
|
||||
if result.Session == nil {
|
||||
log.Fatalf("The server is expected to create sessions for new registrations.")
|
||||
}
|
||||
|
||||
return result.Session, *result.SessionToken
|
||||
}
|
||||
|
||||
func CreateIdentity(c *ory.APIClient) *ory.Identity {
|
||||
ctx := context.Background()
|
||||
|
||||
email, _ := RandomCredentials()
|
||||
identity, _, err := c.IdentityAPI.CreateIdentity(ctx).CreateIdentityBody(ory.CreateIdentityBody{
|
||||
SchemaId: "default",
|
||||
Traits: map[string]interface{}{
|
||||
"email": email,
|
||||
}}).Execute()
|
||||
ExitOnError(err)
|
||||
return identity
|
||||
}
|
||||
36
examples/go/pkg/stub/identity.schema.json
Normal file
36
examples/go/pkg/stub/identity.schema.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"$id": "identity.schema.json",
|
||||
"$schema": "http://json-schema.org/draft-07/schema#",
|
||||
"title": "Person",
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"traits": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
"email": {
|
||||
"type": "string",
|
||||
"format": "email",
|
||||
"title": "E-Mail",
|
||||
"minLength": 3,
|
||||
"ory.sh/kratos": {
|
||||
"credentials": {
|
||||
"password": {
|
||||
"identifier": true
|
||||
}
|
||||
},
|
||||
"verification": {
|
||||
"via": "email"
|
||||
},
|
||||
"recovery": {
|
||||
"via": "email"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"required": [
|
||||
"email"
|
||||
],
|
||||
"additionalProperties": false
|
||||
}
|
||||
}
|
||||
}
|
||||
87
examples/go/pkg/stub/kratos.yaml
Normal file
87
examples/go/pkg/stub/kratos.yaml
Normal file
@@ -0,0 +1,87 @@
|
||||
version: v0.6.3-alpha.1
|
||||
|
||||
dsn: memory
|
||||
|
||||
serve:
|
||||
public:
|
||||
cors:
|
||||
enabled: true
|
||||
|
||||
selfservice:
|
||||
default_browser_return_url: http://127.0.0.1:4455/
|
||||
allowed_return_urls:
|
||||
- http://127.0.0.1:4455
|
||||
|
||||
methods:
|
||||
password:
|
||||
enabled: true
|
||||
link:
|
||||
enabled: true
|
||||
|
||||
flows:
|
||||
error:
|
||||
ui_url: http://127.0.0.1:4455/error
|
||||
|
||||
settings:
|
||||
ui_url: http://127.0.0.1:4455/settings
|
||||
privileged_session_max_age: 15m
|
||||
|
||||
recovery:
|
||||
enabled: true
|
||||
ui_url: http://127.0.0.1:4455/recovery
|
||||
use: link
|
||||
|
||||
verification:
|
||||
enabled: true
|
||||
ui_url: http://127.0.0.1:4455/verify
|
||||
use: link
|
||||
after:
|
||||
default_browser_return_url: http://127.0.0.1:4455/
|
||||
|
||||
logout:
|
||||
after:
|
||||
default_browser_return_url: http://127.0.0.1:4455/auth/login
|
||||
|
||||
login:
|
||||
ui_url: http://127.0.0.1:4455/auth/login
|
||||
lifespan: 10m
|
||||
|
||||
registration:
|
||||
lifespan: 10m
|
||||
ui_url: http://127.0.0.1:4455/auth/registration
|
||||
after:
|
||||
password:
|
||||
hooks:
|
||||
- hook: session
|
||||
|
||||
log:
|
||||
level: debug
|
||||
format: text
|
||||
leak_sensitive_values: true
|
||||
|
||||
secrets:
|
||||
cookie:
|
||||
- PLEASE-CHANGE-ME-I-AM-VERY-INSECURE
|
||||
cipher:
|
||||
- 32-LONG-SECRET-NOT-SECURE-AT-ALL
|
||||
|
||||
ciphers:
|
||||
algorithm: xchacha20-poly1305
|
||||
|
||||
hashers:
|
||||
argon2:
|
||||
parallelism: 1
|
||||
memory: 128MB
|
||||
iterations: 2
|
||||
salt_length: 16
|
||||
key_length: 16
|
||||
|
||||
identity:
|
||||
default_schema_id: default
|
||||
schemas:
|
||||
- id: default
|
||||
url: file://../../pkg/stub/identity.schema.json
|
||||
|
||||
courier:
|
||||
smtp:
|
||||
connection_uri: smtps://test:test@mailslurper:1025/?skip_ssl_verify=true
|
||||
29
examples/go/selfservice/error/main.go
Normal file
29
examples/go/selfservice/error/main.go
Normal file
@@ -0,0 +1,29 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func getError() *ory.FlowError {
|
||||
e, res, err := client.FrontendAPI.GetFlowError(context.Background()).Id("stub:500").Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
return e
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(
|
||||
getError(),
|
||||
)
|
||||
}
|
||||
22
examples/go/selfservice/error/main_test.go
Normal file
22
examples/go/selfservice/error/main_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestError(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
e := getError()
|
||||
require.NotEmpty(t, e.Id)
|
||||
}
|
||||
51
examples/go/selfservice/login/main.go
Normal file
51
examples/go/selfservice/login/main.go
Normal file
@@ -0,0 +1,51 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func performLogin() *ory.SuccessfulNativeLogin {
|
||||
ctx := context.Background()
|
||||
|
||||
// Create a temporary user
|
||||
email, password := pkg.RandomCredentials()
|
||||
_, _ = pkg.CreateIdentityWithSession(client, email, password)
|
||||
|
||||
// Initialize the flow
|
||||
flow, res, err := client.FrontendAPI.CreateNativeLoginFlow(ctx).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
// If you want, print the flow here:
|
||||
//
|
||||
// pkg.PrintJSONPretty(flow)
|
||||
|
||||
// Submit the form
|
||||
result, res, err := client.FrontendAPI.UpdateLoginFlow(ctx).Flow(flow.Id).UpdateLoginFlowBody(
|
||||
ory.UpdateLoginFlowWithPasswordMethodAsUpdateLoginFlowBody(&ory.UpdateLoginFlowWithPasswordMethod{
|
||||
Method: "password",
|
||||
Password: password,
|
||||
PasswordIdentifier: &email,
|
||||
}),
|
||||
).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(
|
||||
performLogin(),
|
||||
)
|
||||
}
|
||||
22
examples/go/selfservice/login/main_test.go
Normal file
22
examples/go/selfservice/login/main_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
result := performLogin()
|
||||
require.NotEmpty(t, result.Session)
|
||||
}
|
||||
34
examples/go/selfservice/logout/main.go
Normal file
34
examples/go/selfservice/logout/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func performLogout() {
|
||||
// Create a temporary user
|
||||
email, password := pkg.RandomCredentials()
|
||||
_, sessionToken := pkg.CreateIdentityWithSession(client, email, password)
|
||||
|
||||
// Log out using session token
|
||||
res, err := client.FrontendAPI.PerformNativeLogout(context.Background()).
|
||||
PerformNativeLogoutBody(ory.PerformNativeLogoutBody{SessionToken: sessionToken}).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
}
|
||||
|
||||
func main() {
|
||||
performLogout()
|
||||
fmt.Println("Logout successful!")
|
||||
}
|
||||
19
examples/go/selfservice/logout/main_test.go
Normal file
19
examples/go/selfservice/logout/main_test.go
Normal file
@@ -0,0 +1,19 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
performLogout()
|
||||
}
|
||||
45
examples/go/selfservice/recovery/main.go
Normal file
45
examples/go/selfservice/recovery/main.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func performRecovery(email string) *ory.RecoveryFlow {
|
||||
ctx := context.Background()
|
||||
|
||||
// Initialize the flow
|
||||
flow, res, err := client.FrontendAPI.CreateNativeRecoveryFlow(ctx).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
// If you want, print the flow here:
|
||||
//
|
||||
// pkg.PrintJSONPretty(flow)
|
||||
|
||||
// Submit the form
|
||||
afterSubmit, res, err := client.FrontendAPI.UpdateRecoveryFlow(ctx).Flow(flow.Id).
|
||||
UpdateRecoveryFlowBody(ory.UpdateRecoveryFlowWithLinkMethodAsUpdateRecoveryFlowBody(&ory.UpdateRecoveryFlowWithLinkMethod{
|
||||
Email: email,
|
||||
Method: "link",
|
||||
})).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return afterSubmit
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(
|
||||
performRecovery("someone@foobar.com"),
|
||||
)
|
||||
}
|
||||
25
examples/go/selfservice/recovery/main_test.go
Normal file
25
examples/go/selfservice/recovery/main_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
ory "github.com/ory/kratos/pkg/httpclient"
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
flow := performRecovery("dev+" + uuid.Must(uuid.NewV4()).String() + "@ory.sh")
|
||||
require.NotEmpty(t, flow.Id)
|
||||
assert.EqualValues(t, ory.RECOVERYFLOWSTATE_SENT_EMAIL, flow.State)
|
||||
}
|
||||
48
examples/go/selfservice/registration/main.go
Normal file
48
examples/go/selfservice/registration/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func initRegistration() *ory.SuccessfulNativeRegistration {
|
||||
ctx := context.Background()
|
||||
|
||||
// Initialize the flow
|
||||
flow, res, err := client.FrontendAPI.CreateNativeRegistrationFlow(ctx).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
// If you want, print the flow here:
|
||||
//
|
||||
// pkg.PrintJSONPretty(flow)
|
||||
|
||||
email, password := pkg.RandomCredentials()
|
||||
|
||||
// Submit the registration flow
|
||||
result, res, err := client.FrontendAPI.UpdateRegistrationFlow(ctx).Flow(flow.Id).UpdateRegistrationFlowBody(
|
||||
ory.UpdateRegistrationFlowWithPasswordMethodAsUpdateRegistrationFlowBody(&ory.UpdateRegistrationFlowWithPasswordMethod{
|
||||
Method: "password",
|
||||
Password: password,
|
||||
Traits: map[string]interface{}{"email": email},
|
||||
}),
|
||||
).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(
|
||||
initRegistration(),
|
||||
)
|
||||
}
|
||||
22
examples/go/selfservice/registration/main_test.go
Normal file
22
examples/go/selfservice/registration/main_test.go
Normal file
@@ -0,0 +1,22 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
result := initRegistration()
|
||||
require.NotEmpty(t, result.Identity.Id)
|
||||
}
|
||||
74
examples/go/selfservice/settings/main.go
Normal file
74
examples/go/selfservice/settings/main.go
Normal file
@@ -0,0 +1,74 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
var ctx = context.Background()
|
||||
|
||||
func initFlow(email, password string) (string, *ory.SettingsFlow) {
|
||||
// Create a temporary user
|
||||
_, sessionToken := pkg.CreateIdentityWithSession(client, email, password)
|
||||
|
||||
flow, res, err := client.FrontendAPI.CreateNativeSettingsFlow(context.Background()).XSessionToken(sessionToken).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
// If you want, print the flow here:
|
||||
//
|
||||
// pkg.PrintJSONPretty(flow)
|
||||
|
||||
return sessionToken, flow
|
||||
}
|
||||
|
||||
func changePassword(email, password string) *ory.SettingsFlow {
|
||||
sessionToken, flow := initFlow(email, password)
|
||||
|
||||
// Submit the form
|
||||
result, res, err := client.FrontendAPI.UpdateSettingsFlow(ctx).Flow(flow.Id).XSessionToken(sessionToken).UpdateSettingsFlowBody(
|
||||
ory.UpdateSettingsFlowWithPasswordMethodAsUpdateSettingsFlowBody(&ory.UpdateSettingsFlowWithPasswordMethod{
|
||||
Method: "password",
|
||||
Password: "not-" + password,
|
||||
}),
|
||||
).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func changeTraits(email, password string) *ory.SettingsFlow {
|
||||
sessionToken, flow := initFlow(email, password)
|
||||
|
||||
// Submit the form
|
||||
result, res, err := client.FrontendAPI.UpdateSettingsFlow(ctx).Flow(flow.Id).XSessionToken(sessionToken).UpdateSettingsFlowBody(
|
||||
ory.UpdateSettingsFlowWithProfileMethodAsUpdateSettingsFlowBody(&ory.UpdateSettingsFlowWithProfileMethod{
|
||||
Method: "profile",
|
||||
Traits: map[string]interface{}{
|
||||
"email": "not-" + email,
|
||||
},
|
||||
}),
|
||||
).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(
|
||||
changePassword(pkg.RandomCredentials()),
|
||||
)
|
||||
pkg.PrintJSONPretty(
|
||||
changeTraits(pkg.RandomCredentials()),
|
||||
)
|
||||
}
|
||||
34
examples/go/selfservice/settings/main_test.go
Normal file
34
examples/go/selfservice/settings/main_test.go
Normal file
@@ -0,0 +1,34 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
ory "github.com/ory/kratos/pkg/httpclient"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestSettings(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
email, password := pkg.RandomCredentials()
|
||||
result := changePassword(email, password)
|
||||
require.NotEmpty(t, result.Id)
|
||||
assert.EqualValues(t, ory.SETTINGSFLOWSTATE_SUCCESS, result.State)
|
||||
|
||||
email, password = pkg.RandomCredentials()
|
||||
result = changeTraits(email, password)
|
||||
require.NotEmpty(t, result.Id)
|
||||
assert.EqualValues(t, ory.SETTINGSFLOWSTATE_SUCCESS, result.State)
|
||||
assert.Equal(t, "not-"+email, result.Identity.Traits.(map[string]interface{})["email"].(string))
|
||||
}
|
||||
45
examples/go/selfservice/verification/main.go
Normal file
45
examples/go/selfservice/verification/main.go
Normal file
@@ -0,0 +1,45 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func performVerification(email string) *ory.VerificationFlow {
|
||||
ctx := context.Background()
|
||||
|
||||
// Initialize the flow
|
||||
flow, res, err := client.FrontendAPI.CreateNativeVerificationFlow(ctx).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
// If you want, print the flow here:
|
||||
//
|
||||
// pkg.PrintJSONPretty(flow)
|
||||
|
||||
// Submit the form
|
||||
afterSubmit, res, err := client.FrontendAPI.UpdateVerificationFlow(ctx).Flow(flow.Id).
|
||||
UpdateVerificationFlowBody(ory.UpdateVerificationFlowWithLinkMethodAsUpdateVerificationFlowBody(&ory.UpdateVerificationFlowWithLinkMethod{
|
||||
Email: email,
|
||||
Method: "link",
|
||||
})).Execute()
|
||||
pkg.SDKExitOnError(err, res)
|
||||
|
||||
return afterSubmit
|
||||
}
|
||||
|
||||
func main() {
|
||||
pkg.PrintJSONPretty(
|
||||
performVerification("someone@foobar.com"),
|
||||
)
|
||||
}
|
||||
25
examples/go/selfservice/verification/main_test.go
Normal file
25
examples/go/selfservice/verification/main_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gofrs/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
ory "github.com/ory/kratos/pkg/httpclient"
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
flow := performVerification("dev+" + uuid.Must(uuid.NewV4()).String() + "@ory.sh")
|
||||
require.NotEmpty(t, flow.Id)
|
||||
assert.EqualValues(t, ory.VERIFICATIONFLOWSTATE_SENT_EMAIL, flow.State)
|
||||
}
|
||||
31
examples/go/session/tosession/main.go
Normal file
31
examples/go/session/tosession/main.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
ory "github.com/ory/client-go"
|
||||
)
|
||||
|
||||
// If you use Open Source this would be:
|
||||
//
|
||||
// var client = pkg.NewSDKForSelfHosted("http://127.0.0.1:4433")
|
||||
var client = pkg.NewSDK("playground")
|
||||
|
||||
func toSession() *ory.Session {
|
||||
// Create a temporary user
|
||||
email, password := pkg.RandomCredentials()
|
||||
_, sessionToken := pkg.CreateIdentityWithSession(client, email, password)
|
||||
|
||||
session, res, err := client.FrontendAPI.ToSessionExecute(ory.FrontendAPIToSessionRequest{}.
|
||||
XSessionToken(sessionToken))
|
||||
pkg.SDKExitOnError(err, res)
|
||||
return session
|
||||
}
|
||||
|
||||
func main() {
|
||||
session := toSession()
|
||||
pkg.PrintJSONPretty(session)
|
||||
}
|
||||
25
examples/go/session/tosession/main_test.go
Normal file
25
examples/go/session/tosession/main_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
// Copyright © 2023 Ory Corp
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/ory/kratos/pkg/testhelpers"
|
||||
|
||||
"github.com/ory/kratos/examples/go/pkg"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestFunc(t *testing.T) {
|
||||
publicURL, _ := testhelpers.StartE2EServer(t, "../../pkg/stub/kratos.yaml", nil)
|
||||
client = pkg.NewSDKForSelfHosted(publicURL)
|
||||
|
||||
session := toSession()
|
||||
require.NotEmpty(t, session.Id)
|
||||
assert.Contains(t, session.Identity.Traits.(map[string]interface{})["email"].(string), "dev+")
|
||||
}
|
||||
Reference in New Issue
Block a user