Skip to content

Commit

Permalink
updating
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-tkachov committed May 4, 2021
1 parent b41899a commit 7e168aa
Show file tree
Hide file tree
Showing 21 changed files with 126 additions and 320 deletions.
26 changes: 26 additions & 0 deletions applications/eva/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ var rootCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(database.Database)
rootCmd.AddCommand(&cobra.Command{
Use: "grpc",
Run: RunGrpc,
})
rootCmd.AddCommand(&cobra.Command{
Use: "http",
Run: RunHttp,
})
}

func main() {
Expand All @@ -32,6 +40,11 @@ func main() {
}

func Run(cmd *cobra.Command, args []string) {
go RunGrpc(cmd, args)
RunHttp(cmd, args)
}

func RunGrpc(cmd *cobra.Command, args []string) {
ctx, cancelFn := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFn()

Expand All @@ -45,3 +58,16 @@ func Run(cmd *cobra.Command, args []string) {
eva.RegisterEvaServer(server, s)
})
}

func RunHttp(cmd *cobra.Command, args []string) {
ctx, cancelFn := context.WithTimeout(context.Background(), time.Second*5)
defer cancelFn()

app, cleanup := service.NewApplication(ctx)

defer cleanup()

srv := ports.NewGraphQLServer(app)

bootstrap.InitializeHttpServer(srv, func() {})
}
46 changes: 0 additions & 46 deletions applications/eva/proto/eva.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,6 @@ syntax = "proto3";

package applications.grpc.eva.v1;

message AuthenticationCookie {
string cookie = 1;
bool redeemed = 2;
string expiration = 3;
string email = 4;
string session = 5;
}

message RedeemCookieResponse {
AuthenticationCookie cookie = 1;
bool registered = 2;
}

message CreateAuthenticationCookieRequest {
string email = 1;
string session = 2;
}

message GetAuthenticationCookieRequest {
string cookie = 1;
}

message RegisterUserRequest {
string cookieId = 1;
string username = 2;
}

message User {
string id = 1;
string username = 2;
Expand All @@ -51,26 +24,7 @@ message Session {
string expiration = 3;
}

message ConsumeCookieResponse {
AuthenticationCookie cookie = 1;
Session session = 2;
}

message Revoke {}

service Eva {
rpc ValidateSession(SessionRequest) returns (Session);
rpc RevokeSession(SessionRequest) returns (Revoke);

// These two will create a session
// RegisterUserFromCookie will allow registration and returns a session
rpc RegisterUserFromCookie(RegisterUserRequest) returns (Session);

// AttemptConsume will only return the session if the user is registered
rpc AttemptConsumeCookie(GetAuthenticationCookieRequest) returns (ConsumeCookieResponse);

rpc GetUser(GetUserRequest) returns (User);

rpc CreateAuthenticationCookie(CreateAuthenticationCookieRequest) returns (AuthenticationCookie);
rpc RedeemAuthenticationCookie(GetAuthenticationCookieRequest) returns (RedeemCookieResponse);
}
28 changes: 16 additions & 12 deletions applications/eva/src/app/command/authenticate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,18 @@ import (
"strings"
"time"

"overdoll/applications/hades/src/app"
"overdoll/applications/hades/src/domain/otp"
"overdoll/libraries/cookie"
"overdoll/applications/eva/src/domain/cookie"
"overdoll/libraries/cookies"
"overdoll/libraries/helpers"
"overdoll/libraries/ksuid"
)

type AuthenticateHandler struct {
eva app.EvaService
cr cookie.Repository
}

func NewAuthenticateHandler(eva app.EvaService) AuthenticateHandler {
return AuthenticateHandler{
eva: eva,
}
func NewAuthenticateHandler(cr cookie.Repository) AuthenticateHandler {
return AuthenticateHandler{cr: cr}
}

func (h AuthenticateHandler) Handle(ctx context.Context, email string) (bool, error) {
Expand All @@ -44,7 +42,13 @@ func (h AuthenticateHandler) Handle(ctx context.Context, email string) (bool, er
}

// Create an authentication cookie
ck, err := h.eva.CreateAuthenticationCookie(ctx, email, string(sessionJson))
instance, err := cookie.NewCookie(ksuid.New().String(), email, string(sessionJson))

if err != nil {
return false, err
}

err = h.cr.CreateCookie(ctx, instance)

if err != nil {
return false, err
Expand All @@ -53,9 +57,9 @@ func (h AuthenticateHandler) Handle(ctx context.Context, email string) (bool, er
// OTP login cookie - will determine if
// Opened in the same browser - log them in that browser if this cookie exists
// Otherwise, if opened in another browser (such as the phone), it will log them in on the original browser through a subscription
_, err = cookie.SetCookie(ctx, &http.Cookie{
Name: otp.OTPKey,
Value: ck,
_, err = cookies.SetCookie(ctx, &http.Cookie{
Name: cookie.OTPKey,
Value: instance.Cookie(),
Expires: time.Now().Add(5 * time.Minute),
})

Expand Down
21 changes: 11 additions & 10 deletions applications/eva/src/app/command/check_authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,25 @@ import (
"context"
"net/http"

"overdoll/applications/hades/src/app"
"overdoll/applications/hades/src/domain/otp"
"overdoll/applications/hades/src/domain/user"
"overdoll/applications/hades/src/ports/graphql/types"
"overdoll/libraries/cookie"
"overdoll/applications/eva/src/domain/cookie"
"overdoll/applications/eva/src/domain/user"
"overdoll/applications/eva/src/ports/graphql/types"
"overdoll/libraries/common"
"overdoll/libraries/cookies"
"overdoll/libraries/helpers"
)

type AuthenticationHandler struct {
eva app.EvaService
cr cookie.Repository
ur user.Repository
}

func NewAuthenticationHandler(eva app.EvaService) AuthenticationHandler {
return AuthenticationHandler{eva: eva}
}

func (h AuthenticationHandler) Handle(ctx context.Context) (*types.Authentication, error) {
usr := user.FromContext(ctx)
usr := common.FromContext(ctx)

gc := helpers.GinContextFromContext(ctx)

Expand All @@ -31,7 +32,7 @@ func (h AuthenticationHandler) Handle(ctx context.Context) (*types.Authenticatio
}

// User is not logged in, let's check for an OTP token
otpCookie, err := cookie.ReadCookie(ctx, otp.OTPKey)
otpCookie, err := cookies.ReadCookie(ctx, cookie.OTPKey)

// Error
if err != nil {
Expand All @@ -52,7 +53,7 @@ func (h AuthenticationHandler) Handle(ctx context.Context) (*types.Authenticatio
// Cookie doesn't exist, remove it

// TODO: only remove cookie if the response indicates that the cookie is expired or invalid- server errors will be ignored
http.SetCookie(gc.Writer, &http.Cookie{Name: otp.OTPKey, Value: "", MaxAge: -1, HttpOnly: true, Secure: true, Path: "/"})
http.SetCookie(gc.Writer, &http.Cookie{Name: cookie.OTPKey, Value: "", MaxAge: -1, HttpOnly: true, Secure: true, Path: "/"})

return &types.Authentication{User: nil, Cookie: nil}, nil
}
Expand All @@ -71,7 +72,7 @@ func (h AuthenticationHandler) Handle(ctx context.Context) (*types.Authenticatio
}

// Remove OTP cookie - no longer needed at this step
http.SetCookie(gc.Writer, &http.Cookie{Name: otp.OTPKey, Value: "", MaxAge: -1, HttpOnly: true, Secure: true, Path: "/"})
http.SetCookie(gc.Writer, &http.Cookie{Name: cookie.OTPKey, Value: "", MaxAge: -1, HttpOnly: true, Secure: true, Path: "/"})

// TODO: set session cookie here from ck.Session.Token

Expand Down
34 changes: 0 additions & 34 deletions applications/eva/src/app/command/create_cookie.go

This file was deleted.

41 changes: 0 additions & 41 deletions applications/eva/src/app/command/create_session.go

This file was deleted.

27 changes: 0 additions & 27 deletions applications/eva/src/app/command/get_user_session.go

This file was deleted.

19 changes: 13 additions & 6 deletions applications/eva/src/app/command/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,28 @@ package command
import (
"context"

"overdoll/applications/hades/src/app"
"overdoll/applications/eva/src/domain/session"
"overdoll/applications/eva/src/domain/user"
)

type LogoutHandler struct {
eva app.EvaService
ur user.Repository
sr session.Repository
}

func NewLogoutHandler(eva app.EvaService) LogoutHandler {
return LogoutHandler{eva: eva}
func NewLogoutHandler(ur user.Repository, sr session.Repository) LogoutHandler {
return LogoutHandler{ur: ur, sr: sr}
}

func (h LogoutHandler) Handle(ctx context.Context) (bool, error) {

// TODO: get session from cookie to determine what to revoke
err := h.eva.RevokeSession(ctx, "id")
sess, err := session.NewSessionFromToken(token)

if err != nil {
return false, err
}

err = h.sr.RevokeSession(ctx, sess)

if err != nil {
return false, err
Expand Down
7 changes: 3 additions & 4 deletions applications/eva/src/app/command/redeem_cookie.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import (

"overdoll/applications/eva/src/domain/cookie"
"overdoll/applications/eva/src/domain/user"
"overdoll/applications/eva/src/ports/graphql/types"
"overdoll/applications/hades/src/domain/otp"
"overdoll/libraries/cookies"
"overdoll/libraries/helpers"
)

Expand All @@ -22,8 +24,6 @@ func NewRedeemCookieHandler(cr cookie.Repository, ur user.Repository) RedeemCook

func (h RedeemCookieHandler) Handle(ctx context.Context, id string) (*cookie.Cookie, error) {

// GRAPHQL

// RedeemCookie - this is when the user uses the redeemed cookie. This will
// occur when the user uses the redeemed cookie in the same browser that has the 'otp-cookie' cookie

Expand All @@ -34,7 +34,7 @@ func (h RedeemCookieHandler) Handle(ctx context.Context, id string) (*cookie.Coo

gc := helpers.GinContextFromContext(ctx)

currentCookie, err := cookie.ReadCookie(ctx, otp.OTPKey)
currentCookie, err := cookies.ReadCookie(ctx, cookie.OTPKey)

if err != nil {

Expand Down Expand Up @@ -104,7 +104,6 @@ func (h RedeemCookieHandler) Handle(ctx context.Context, id string) (*cookie.Coo
Invalid: false,
}, err


// RPC

ck, err := h.cr.GetCookieById(ctx, id)
Expand Down

0 comments on commit 7e168aa

Please sign in to comment.