Files
paralus/_kratos/development/pkg/resources.go
nirav-rafay c66bdc25cd restructure rcloud-base as a single base controller (#37)
* restructure rcloud-base as a single base controller
* updated master.rest
* moved sentry from internal to pkg as it is used by relay
* removing unused rpc and it's dependencies
* Fix usermgmt tests
* Don't redefine variables in rest file
Co-authored-by: Abin Simon <abin.simon@rafay.co>
2022-03-03 17:59:06 +05:30

69 lines
1.8 KiB
Go

package pkg
import (
"context"
"log"
"strings"
"github.com/google/uuid"
ory "github.com/ory/kratos-client-go"
)
func RandomCredentials() (email, password string) {
email = "dev+" + uuid.New().String() + "@ory.sh"
password = strings.ReplaceAll(uuid.New().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.V0alpha2Api.InitializeSelfServiceRegistrationFlowWithoutBrowser(ctx).Execute()
ExitOnError(err)
// Submit the registration flow
result, res, err := c.V0alpha2Api.SubmitSelfServiceRegistrationFlow(ctx).Flow(flow.Id).SubmitSelfServiceRegistrationFlowBody(
ory.SubmitSelfServiceRegistrationFlowWithPasswordMethodBodyAsSubmitSelfServiceRegistrationFlowBody(&ory.SubmitSelfServiceRegistrationFlowWithPasswordMethodBody{
Method: "password",
Password: password,
Traits: map[string]interface{}{
"email": email,
"first_name": "Jon",
"last_name": "Doe",
"description": "nothing",
},
}),
).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.V0alpha2Api.AdminCreateIdentity(ctx).AdminCreateIdentityBody(ory.AdminCreateIdentityBody{
SchemaId: "default",
Traits: map[string]interface{}{
"email": email,
}}).Execute()
ExitOnError(err)
return identity
}