2019-04-15 03:56:55 +00:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/ptypes"
|
2019-05-28 01:16:50 +00:00
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
protobuf "github.com/mitchell/selfpass/protobuf/go"
|
2019-07-11 02:33:22 +00:00
|
|
|
"github.com/mitchell/selfpass/services/credentials/endpoints"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
2019-04-15 03:56:55 +00:00
|
|
|
)
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
func decodeSourceHostRequest(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
r := request.(protobuf.SourceHostRequest)
|
|
|
|
return endpoints.SourceHostRequest{
|
2019-04-15 03:56:55 +00:00
|
|
|
SourceHost: r.SourceHost,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-07-16 06:14:54 +00:00
|
|
|
func EncodeSourceHostRequest(request endpoints.SourceHostRequest) *protobuf.SourceHostRequest {
|
|
|
|
return &protobuf.SourceHostRequest{
|
|
|
|
SourceHost: request.SourceHost,
|
|
|
|
}
|
2019-05-22 15:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 03:56:55 +00:00
|
|
|
func encodeMetadataStreamResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
|
|
|
r := response.(endpoints.MetadataStream)
|
|
|
|
pbmdch := make(chan protobuf.Metadata, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(pbmdch)
|
|
|
|
|
|
|
|
for md := range r.Metadata {
|
|
|
|
createdAt, err := ptypes.TimestampProto(md.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
r.Errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAt, err := ptypes.TimestampProto(md.UpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
r.Errors <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
pbmdch <- protobuf.Metadata{
|
|
|
|
Id: md.ID,
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
SourceHost: md.SourceHost,
|
|
|
|
Primary: md.Primary,
|
|
|
|
LoginUrl: md.LoginURL,
|
2019-05-22 15:22:40 +00:00
|
|
|
Tag: md.Tag,
|
2019-04-15 03:56:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
return ProtobufMetadataStream{
|
2019-04-15 03:56:55 +00:00
|
|
|
Metadata: pbmdch,
|
|
|
|
Errors: r.Errors,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
func DecodeMetdataStreamResponse(ctx context.Context, r ProtobufMetadataStream) (endpoints.MetadataStream, error) {
|
|
|
|
mdch := make(chan types.Metadata, 1)
|
|
|
|
errch := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(mdch)
|
|
|
|
|
|
|
|
for pbmd := range r.Metadata {
|
|
|
|
createdAt, err := ptypes.Timestamp(pbmd.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAt, err := ptypes.Timestamp(pbmd.UpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mdch <- types.Metadata{
|
|
|
|
ID: pbmd.Id,
|
|
|
|
SourceHost: pbmd.SourceHost,
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
Primary: pbmd.Primary,
|
|
|
|
LoginURL: pbmd.LoginUrl,
|
|
|
|
Tag: pbmd.Tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return endpoints.MetadataStream{
|
|
|
|
Metadata: mdch,
|
|
|
|
Errors: errch,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ProtobufMetadataStream struct {
|
2019-04-15 03:56:55 +00:00
|
|
|
Metadata <-chan protobuf.Metadata
|
|
|
|
Errors chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
func decodeCredentialRequest(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
r := request.(protobuf.CredentialRequest)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-04-15 03:56:55 +00:00
|
|
|
return types.CredentialInput{
|
|
|
|
MetadataInput: types.MetadataInput{
|
|
|
|
Primary: r.Primary,
|
|
|
|
LoginURL: r.LoginUrl,
|
|
|
|
SourceHost: r.SourceHost,
|
2019-05-22 15:22:40 +00:00
|
|
|
Tag: r.Tag,
|
2019-04-15 03:56:55 +00:00
|
|
|
},
|
2019-05-28 01:16:50 +00:00
|
|
|
Username: r.Username,
|
|
|
|
Email: r.Email,
|
|
|
|
Password: r.Password,
|
|
|
|
OTPSecret: r.OtpSecret,
|
2019-04-15 03:56:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
func EncodeCredentialRequest(r types.CredentialInput) protobuf.CredentialRequest {
|
|
|
|
return protobuf.CredentialRequest{
|
|
|
|
Primary: r.Primary,
|
|
|
|
Username: r.Username,
|
|
|
|
Email: r.Email,
|
|
|
|
Password: r.Password,
|
2019-05-28 01:16:50 +00:00
|
|
|
OtpSecret: r.OTPSecret,
|
2019-05-22 15:22:40 +00:00
|
|
|
SourceHost: r.SourceHost,
|
|
|
|
LoginUrl: r.LoginURL,
|
|
|
|
Tag: r.Tag,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-15 03:56:55 +00:00
|
|
|
func encodeCredentialResponse(ctx context.Context, response interface{}) (interface{}, error) {
|
|
|
|
r := response.(types.Credential)
|
|
|
|
|
|
|
|
createdAt, err := ptypes.TimestampProto(r.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAt, err := ptypes.TimestampProto(r.UpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return protobuf.Credential{
|
|
|
|
Id: r.ID,
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
Primary: r.Primary,
|
|
|
|
SourceHost: r.SourceHost,
|
|
|
|
LoginUrl: r.LoginURL,
|
2019-05-22 15:22:40 +00:00
|
|
|
Tag: r.Tag,
|
2019-04-15 03:56:55 +00:00
|
|
|
Username: r.Username,
|
|
|
|
Email: r.Email,
|
|
|
|
Password: r.Password,
|
2019-05-28 01:16:50 +00:00
|
|
|
OtpSecret: r.OTPSecret,
|
2019-04-15 03:56:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
func DecodeCredential(r protobuf.Credential) (c types.Credential, err error) {
|
|
|
|
|
|
|
|
createdAt, err := ptypes.Timestamp(r.CreatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
updatedAt, err := ptypes.Timestamp(r.UpdatedAt)
|
|
|
|
if err != nil {
|
|
|
|
return c, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return types.Credential{
|
|
|
|
Metadata: types.Metadata{
|
|
|
|
ID: r.Id,
|
|
|
|
SourceHost: r.SourceHost,
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
UpdatedAt: updatedAt,
|
|
|
|
Primary: r.Primary,
|
|
|
|
LoginURL: r.LoginUrl,
|
|
|
|
Tag: r.Tag,
|
|
|
|
},
|
2019-05-28 01:16:50 +00:00
|
|
|
Username: r.Username,
|
|
|
|
Email: r.Email,
|
|
|
|
Password: r.Password,
|
|
|
|
OTPSecret: r.OtpSecret,
|
2019-05-22 15:22:40 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-04-15 03:56:55 +00:00
|
|
|
func decodeUpdateRequest(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
r := request.(protobuf.UpdateRequest)
|
2019-05-22 15:22:40 +00:00
|
|
|
|
2019-04-15 03:56:55 +00:00
|
|
|
return endpoints.UpdateRequest{
|
|
|
|
ID: r.Id,
|
|
|
|
Credential: types.CredentialInput{
|
|
|
|
MetadataInput: types.MetadataInput{
|
|
|
|
Primary: r.Credential.Primary,
|
|
|
|
SourceHost: r.Credential.SourceHost,
|
|
|
|
LoginURL: r.Credential.LoginUrl,
|
2019-05-22 15:22:40 +00:00
|
|
|
Tag: r.Credential.Tag,
|
2019-04-15 03:56:55 +00:00
|
|
|
},
|
2019-05-28 01:16:50 +00:00
|
|
|
Username: r.Credential.Username,
|
|
|
|
Email: r.Credential.Email,
|
|
|
|
Password: r.Credential.Password,
|
|
|
|
OTPSecret: r.Credential.OtpSecret,
|
2019-04-15 03:56:55 +00:00
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
func EncodeUpdateRequest(r endpoints.UpdateRequest) protobuf.UpdateRequest {
|
2019-05-22 15:22:40 +00:00
|
|
|
c := r.Credential
|
|
|
|
return protobuf.UpdateRequest{
|
|
|
|
Id: r.ID,
|
|
|
|
Credential: &protobuf.CredentialRequest{
|
|
|
|
Primary: c.Primary,
|
|
|
|
Username: c.Username,
|
|
|
|
Email: c.Email,
|
|
|
|
Password: c.Password,
|
2019-05-28 01:16:50 +00:00
|
|
|
OtpSecret: c.OTPSecret,
|
2019-05-22 15:22:40 +00:00
|
|
|
SourceHost: c.SourceHost,
|
|
|
|
LoginUrl: c.LoginURL,
|
|
|
|
Tag: c.Tag,
|
|
|
|
},
|
2019-05-28 01:16:50 +00:00
|
|
|
}
|
2019-05-22 15:22:40 +00:00
|
|
|
}
|
|
|
|
|
2019-04-15 03:56:55 +00:00
|
|
|
func decodeIdRequest(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
r := request.(protobuf.IdRequest)
|
|
|
|
return endpoints.IDRequest{
|
|
|
|
ID: r.Id,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
func EncodeIdRequest(r endpoints.IDRequest) protobuf.IdRequest {
|
|
|
|
return protobuf.IdRequest{
|
|
|
|
Id: r.ID,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func noOp(context.Context, interface{}) (interface{}, error) {
|
2019-04-15 03:56:55 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|