func (m *Machine) CreateToken(ctx context.Context, notifier *observer.Notifier,
	machine *envelope.Unsigned, secret *base64.Value) (*registry.MachineTokenCreationSegment, error) {
	n := notifier.Notifier(2)

	n.Notify(observer.Progress, "Generating machine token", true)
	salt, err := crypto.GenerateSalt(ctx)
	if err != nil {
		return nil, err
	}

	keypair, err := crypto.DeriveLoginKeypair(ctx, secret, salt)
	if err != nil {
		return nil, err
	}

	machineBody, ok := machine.Body.(*primitive.Machine)
	if !ok {
		return nil, &apitypes.Error{
			Type: apitypes.InternalServerError,
			Err:  []string{"Could not cast to Machine"},
		}
	}
	orgID := machineBody.OrgID

	masterKey, err := crypto.CreateMasterKeyObject(ctx, *secret)
	if err != nil {
		return nil, err
	}

	tokenBody := &primitive.MachineToken{
		OrgID:     orgID,
		MachineID: machine.ID,
		PublicKey: &primitive.MachineTokenPublicKey{
			Salt:  keypair.Salt(),
			Value: keypair.PublicKey(),
			Alg:   crypto.EdDSA,
		},
		Master:      masterKey,
		CreatedBy:   m.engine.session.ID(),
		Created:     time.Now().UTC(),
		DestroyedBy: nil,
		Destroyed:   nil,
		State:       primitive.MachineTokenActiveState,
	}
	tokenID, err := identity.NewMutable(tokenBody)
	if err != nil {
		return nil, err
	}

	token := &envelope.Unsigned{
		ID:      &tokenID,
		Version: 1,
		Body:    tokenBody,
	}

	// Create an "empty" machine session in order to create a Crypto engine on
	// behalf of the machine for deriving and uploading these keys.
	sess := session.NewSession()
	err = sess.Set(apitypes.MachineSession, machine, token, *secret, "asdfsdf")
	if err != nil {
		return nil, err
	}
	c := crypto.NewEngine(sess)

	n.Notify(observer.Progress, "Generating token keypairs", true)
	kp, err := c.GenerateKeyPairs(ctx)
	if err != nil {
		log.Printf("Error generating machine keypairs: %s", err)
		return nil, err
	}

	authID := sess.AuthID()
	keypairs, err := generateKeypairs(ctx, c, orgID, authID, kp)
	if err != nil {
		return nil, err
	}

	return &registry.MachineTokenCreationSegment{
		Token:    token,
		Keypairs: keypairs,
	}, nil
}
// CreateToken generates a new machine token given a machine and a secret value.
CreateToken generates a new machine token given a machine and a secret value.
// CreateToken generates a new machine token given a machine and a secret value .
func (m *Machine) CreateToken(ctx context.Context, notifier *observer.Notifier,
	machine *envelope.Unsigned, secret *base64.Value) (*registry.MachineTokenCreationSegment, error) {
	n := notifier.Notifier(2)
	n.Notify(observer.Progress, "Generating machine token", true)
	salt, err := crypto.GenerateSalt(ctx)
	if err != nil {
		return nil, err
	}
	keypair, err := crypto.DeriveLoginKeypair(ctx, secret, salt)
	if err != nil {
		return nil, err
	}
	machineBody, ok := machine.Body.(*primitive.Machine)
	if !ok {
		return nil, &apitypes.Error{
			Type: apitypes.InternalServerError,
			Err:  []string{"Could not cast to Machine"},
		}
	}
	orgID := machineBody.OrgID
	masterKey, err := crypto.CreateMasterKeyObject(ctx, *secret)
	if err != nil {
		return nil, err
	}
	tokenBody := &primitive.MachineToken{
		OrgID:     orgID,
		MachineID: machine.ID,
		PublicKey: &primitive.MachineTokenPublicKey{
			Salt:  keypair.Salt(),
			Value: keypair.PublicKey(),
			Alg:   crypto.EdDSA,
		},
		Master:      masterKey,
		CreatedBy:   m.engine.session.ID(),
		Created:     time.Now().UTC(),
		DestroyedBy: nil,
		Destroyed:   nil,
		State:       primitive.MachineTokenActiveState,
	}
	tokenID, err := identity.NewMutable(tokenBody)
	if err != nil {
		return nil, err
	}
	token := &envelope.Unsigned{
		ID:      &tokenID,
		Version: 1,
		Body:    tokenBody,
	}
	sess := session.NewSession()
	err = sess.Set(apitypes.MachineSession, machine, token, *secret, "asdfsdf")
	if err != nil {
		return nil, err
	}
	c := crypto.NewEngine(sess)
	n.Notify(observer.Progress, "Generating token keypairs", true)
	kp, err := c.GenerateKeyPairs(ctx)
	if err != nil {
		log.Printf("Error generating machine keypairs: %s", err)
		return nil, err
	}
	authID := sess.AuthID()
	keypairs, err := generateKeypairs(ctx, c, orgID, authID, kp)
	if err != nil {
		return nil, err
	}
	return &registry.MachineTokenCreationSegment{
		Token:    token,
		Keypairs: keypairs,
	}, nil
}
func ( m * Machine ) CreateToken ( ctx context . Context , notifier * observer . Notifier , machine * envelope . Unsigned , secret * base64 . Value ) ( * registry . MachineTokenCreationSegment , error ) { n := notifier . Notifier ( 2 ) n . Notify ( observer . Progress , " " , true ) salt , err := crypto . GenerateSalt ( ctx ) if err != nil { return nil , err } keypair , err := crypto . DeriveLoginKeypair ( ctx , secret , salt ) if err != nil { return nil , err } machineBody , ok := machine . Body . ( * primitive . Machine ) if ! ok { return nil , & apitypes . Error { Type : apitypes . InternalServerError , Err : [ ] string { " " } , } } orgID := machineBody . OrgID masterKey , err := crypto . CreateMasterKeyObject ( ctx , * secret ) if err != nil { return nil , err } tokenBody := & primitive . MachineToken { OrgID : orgID , MachineID : machine . ID , PublicKey : & primitive . MachineTokenPublicKey { Salt : keypair . Salt ( ) , Value : keypair . PublicKey ( ) , Alg : crypto . EdDSA , } , Master : masterKey , CreatedBy : m . engine . session . ID ( ) , Created : time . Now ( ) . UTC ( ) , DestroyedBy : nil , Destroyed : nil , State : primitive . MachineTokenActiveState , } tokenID , err := identity . NewMutable ( tokenBody ) if err != nil { return nil , err } token := & envelope . Unsigned { ID : & tokenID , Version : 1 , Body : tokenBody , } sess := session . NewSession ( ) err = sess . Set ( apitypes . MachineSession , machine , token , * secret , " " ) if err != nil { return nil , err } c := crypto . NewEngine ( sess ) n . Notify ( observer . Progress , " " , true ) kp , err := c . GenerateKeyPairs ( ctx ) if err != nil { log . Printf ( " " , err ) return nil , err } authID := sess . AuthID ( ) keypairs , err := generateKeypairs ( ctx , c , orgID , authID , kp ) if err != nil { return nil , err } return & registry . MachineTokenCreationSegment { Token : token , Keypairs : keypairs , } , nil }
CreateToken generates a new machine token given a machine and a secret value.
CreateToken generates a new machine token given a machine and a secret value .
// Create an "empty" machine session in order to create a Crypto engine on // behalf of the machine for deriving and uploading these keys.