Skip to content

Commit

Permalink
service: Twitter proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel committed Dec 21, 2020
1 parent 7c9e6e8 commit cda646b
Show file tree
Hide file tree
Showing 10 changed files with 679 additions and 643 deletions.
4 changes: 2 additions & 2 deletions service/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ require (
github.com/golang/snappy v0.0.2 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.2.2
github.com/keybase/go-keychain v0.0.0-20201121013009-976c83ec27a6 // indirect
github.com/keys-pub/keys v0.1.18-0.20201221024928-926fad6581ab
github.com/keys-pub/keys v0.1.19-0.20201221195739-7490f3244390
github.com/keys-pub/keys-ext/auth/fido2 v0.0.0-20201218004539-7f8afbf3d488
github.com/keys-pub/keys-ext/auth/mock v0.0.0-20201018000238-7b6186f1fe97
github.com/keys-pub/keys-ext/http/api v0.0.0-20201218211059-81db8e866f8c
github.com/keys-pub/keys-ext/http/client v0.0.0-20201221025613-72a657ea35c1
github.com/keys-pub/keys-ext/http/server v0.0.0-20201221022604-418ba635ab03
github.com/keys-pub/keys-ext/http/server v0.0.0-20201221200600-7c9e6e8730aa
github.com/keys-pub/keys-ext/sdb v0.0.0-20201218004539-7f8afbf3d488
github.com/keys-pub/keys-ext/vault v0.0.0-20201221025816-0324f4b664dc
github.com/keys-pub/keys-ext/wormhole v0.0.0-20201218004539-7f8afbf3d488
Expand Down
4 changes: 4 additions & 0 deletions service/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,8 @@ github.com/keys-pub/keys v0.1.18-0.20201218205715-618ff87d91d4/go.mod h1:CgoPVi0
github.com/keys-pub/keys v0.1.18-0.20201221022455-0512e1665806/go.mod h1:CgoPVi0AZw87ncg5xCYnhlY9h+WQmAVsokthREKygzw=
github.com/keys-pub/keys v0.1.18-0.20201221024928-926fad6581ab h1:wFVsXqFs5FvDBgF+SiOpcsgaTTYN0bVA7UIhNZYIKV4=
github.com/keys-pub/keys v0.1.18-0.20201221024928-926fad6581ab/go.mod h1:CgoPVi0AZw87ncg5xCYnhlY9h+WQmAVsokthREKygzw=
github.com/keys-pub/keys v0.1.19-0.20201221195739-7490f3244390 h1:Afr5cTfUV+DWnR5tKZuD113kQ3deE7IYgvhcvbaHwmQ=
github.com/keys-pub/keys v0.1.19-0.20201221195739-7490f3244390/go.mod h1:CgoPVi0AZw87ncg5xCYnhlY9h+WQmAVsokthREKygzw=
github.com/keys-pub/keys-ext v0.1.20 h1:KZs+L8GtJj00bKh+xhQqHNkSQgq2fUCYwBsrP2vV23g=
github.com/keys-pub/keys-ext v0.1.21-0.20201023185358-3aa021ac522b h1:KtnQeluO4nhs2SRB71vnQcSUKDMsW6fzTjHUQlXss6o=
github.com/keys-pub/keys-ext v0.1.21-0.20201027204032-04296a431295 h1:J5pKPITbp2yYiDkvTWCoo4Wh9bhnJ1BNlo5MpgxgyNA=
Expand Down Expand Up @@ -352,6 +354,8 @@ github.com/keys-pub/keys-ext/http/server v0.0.0-20201217212639-fc124fb22380 h1:K
github.com/keys-pub/keys-ext/http/server v0.0.0-20201217212639-fc124fb22380/go.mod h1:TdKZFVBsWl7X/dr0wAYVQRe9N01Ce0RKU5y/2yF1fiI=
github.com/keys-pub/keys-ext/http/server v0.0.0-20201221022604-418ba635ab03 h1:L/htdIBq1bd+VTx5TOQWOpUyuH5LEvahV1IbY+DWiGU=
github.com/keys-pub/keys-ext/http/server v0.0.0-20201221022604-418ba635ab03/go.mod h1:mBula0pl025c9LGKzhJY9lXvVbWvuWXl8IM8xmfg3x0=
github.com/keys-pub/keys-ext/http/server v0.0.0-20201221200600-7c9e6e8730aa h1:Av792pn0RaKHHvYMSzov6WhXhzXQ+Vv1M10BjubW0/I=
github.com/keys-pub/keys-ext/http/server v0.0.0-20201221200600-7c9e6e8730aa/go.mod h1:F0+G7tXZOvUyfoAArR3W5kXcrri6WQzGQ/gtTWMOJ9E=
github.com/keys-pub/keys-ext/sdb v0.0.0-20200917215110-bda938100d21 h1:kQE/wWdNcKgXmR/PLjPIsh89Q1Wf0/wWUd5JYSMTSBI=
github.com/keys-pub/keys-ext/sdb v0.0.0-20200917215110-bda938100d21/go.mod h1:a2kdlrU3Q860hDBlcpzqEzXMSQPh6C1rXdXmyVP8MGo=
github.com/keys-pub/keys-ext/sdb v0.0.0-20201023200942-48e5f880045e h1:kwFLA7TqxFsGjpwEdxdRkyLY6YqiQHCEh7/Ra7SOGcM=
Expand Down
2 changes: 1 addition & 1 deletion service/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (s *service) KeyImport(ctx context.Context, req *KeyImportRequest) (*KeyImp
return nil, err
}

if _, err := s.updateUser(ctx, key.ID()); err != nil {
if _, err := s.updateUser(ctx, key.ID(), false); err != nil {
return nil, err
}

Expand Down
2 changes: 1 addition & 1 deletion service/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func (s *service) Key(ctx context.Context, req *KeyRequest) (*KeyResponse, error
}

if req.Update {
if _, err := s.updateUser(ctx, kid); err != nil {
if _, err := s.updateUser(ctx, kid, false); err != nil {
return nil, err
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion service/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,5 @@ func (s *service) userResultOrUpdate(ctx context.Context, kid keys.ID) (*user.Re
if user != nil {
return s.users.Get(ctx, kid)
}
return s.updateUser(ctx, kid)
return s.updateUser(ctx, kid, false)
}
1,266 changes: 638 additions & 628 deletions service/keys.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions service/keys.proto
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ message User {
UserStatus status = 10;
int64 verifiedAt = 11;
int64 timestamp = 12;
bool proxied = 13;

string err = 20;
}
Expand Down
2 changes: 1 addition & 1 deletion service/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (s *service) pullUser(ctx context.Context, kid keys.ID) (*user.Result, erro
if err := s.importID(kid); err != nil {
return nil, err
}
res, err := s.updateUser(ctx, kid)
res, err := s.updateUser(ctx, kid, false)
if err != nil {
return nil, err
}
Expand Down
28 changes: 24 additions & 4 deletions service/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/keys-pub/keys"
"github.com/keys-pub/keys/user"
"github.com/keys-pub/keys/user/services"
"github.com/keys-pub/keys/users"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -45,7 +46,7 @@ func (s *service) checkForExpiredKey(ctx context.Context, kid keys.ID) error {
now := s.clock.Now()
if (res.Status != user.StatusOK && res.IsTimestampExpired(now, userCheckFailureExpire)) ||
res.IsTimestampExpired(now, userCheckExpire) {
_, err := s.updateUser(ctx, kid)
_, err := s.updateUser(ctx, kid, true)
if err != nil {
return err
}
Expand All @@ -61,14 +62,14 @@ func (s *service) updateAllKeys(ctx context.Context) error {
return err
}
for _, pk := range pks {
if _, err := s.updateUser(ctx, pk.ID()); err != nil {
if _, err := s.updateUser(ctx, pk.ID(), true); err != nil {
return err
}
}
return nil
}

func (s *service) updateUser(ctx context.Context, kid keys.ID) (*user.Result, error) {
func (s *service) updateUser(ctx context.Context, kid keys.ID, allowProxyCache bool) (*user.Result, error) {
logger.Infof("Update user %s", kid)

// TODO: Only get new sigchain entries.
Expand All @@ -95,10 +96,29 @@ func (s *service) updateUser(ctx context.Context, kid keys.ID) (*user.Result, er
return nil, err
}

res, err := s.users.Update(ctx, kid, users.UseTwitterProxy())
service := func(usr *user.User) services.Service {
switch usr.Service {
case "twitter":
if allowProxyCache {
return services.KeysPub
}
return services.Proxy
}
return nil
}

res, err := s.users.Update(ctx, kid, users.UseService(service))
if err != nil {
return nil, err
}

return res, nil
}

func twitterProxy(usr *user.User) services.Service {
switch usr.Service {
case "twitter":
return services.Proxy
}
return nil
}
11 changes: 6 additions & 5 deletions service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s *service) User(ctx context.Context, req *UserRequest) (*UserResponse, er
return nil, err
}
if resp != nil {
res, err := s.updateUser(ctx, resp.User.KID)
res, err := s.updateUser(ctx, resp.User.KID, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -192,7 +192,7 @@ func (s *service) sigchainUserAdd(ctx context.Context, key *keys.EdX25519Key, se
return nil, nil, err
}

userService, err := users.LookupService(service, users.UseTwitterProxy(), users.IsCreate())
userService, err := users.LookupService(usr, users.UseService(twitterProxy))
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -227,7 +227,7 @@ func (s *service) sigchainUserAdd(ctx context.Context, key *keys.EdX25519Key, se
return nil, nil, err
}

if _, err = s.users.Update(ctx, key.ID(), users.UseTwitterProxy(), users.IsCreate()); err != nil {
if _, err = s.users.Update(ctx, key.ID(), users.UseService(twitterProxy)); err != nil {
return nil, nil, err
}

Expand Down Expand Up @@ -283,6 +283,7 @@ func userResultToRPC(result *user.Result) *User {
Status: userStatus(result.Status),
VerifiedAt: result.VerifiedAt,
Timestamp: result.Timestamp,
Proxied: result.Proxied,
Err: result.Err,
}
}
Expand Down Expand Up @@ -325,7 +326,7 @@ func (s *service) searchRemoteCheckUser(ctx context.Context, query string) (*Use
if user.ID != query {
return nil, errors.Errorf("user search mismatch %s != %s", user.ID, query)
}
res, err := s.updateUser(ctx, keys.ID(user.KID))
res, err := s.updateUser(ctx, keys.ID(user.KID), true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -405,7 +406,7 @@ func (s *service) ensureUserVerified(ctx context.Context, kid keys.ID) error {

// Our verify expired, re-check.
logger.Infof("Checking user %v", res)
resNew, err := s.updateUser(ctx, res.User.KID)
resNew, err := s.updateUser(ctx, res.User.KID, true)
if err != nil {
return err
}
Expand Down

0 comments on commit cda646b

Please sign in to comment.