Skip to content

Commit

Permalink
feat: add session cookies
Browse files Browse the repository at this point in the history
Signed-off-by: maxwellgithinji <maxwellgithinji@gmail.com>
  • Loading branch information
maxwellgithinji committed Apr 26, 2023
1 parent 97ed6bf commit 6d5eca0
Show file tree
Hide file tree
Showing 17 changed files with 1,853 additions and 65 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
firebase.google.com/go v3.13.0+incompatible
github.com/99designs/gqlgen v0.17.26
github.com/GoogleCloudPlatform/cloudsql-proxy v1.33.0
github.com/alexedwards/scs/v2 v2.5.1
github.com/basgys/goxml2json v1.1.0
github.com/brianvoe/gofakeit v3.18.0+incompatible
github.com/cenkalti/backoff/v4 v4.1.3
Expand Down Expand Up @@ -48,6 +49,7 @@ require (
github.com/xdg-go/pbkdf2 v1.0.0
go.opencensus.io v0.24.0
golang.org/x/crypto v0.0.0-20221012134737-56aed061732a
golang.org/x/text v0.8.0
google.golang.org/api v0.103.0
gopkg.in/go-playground/validator.v9 v9.31.0
gorm.io/driver/postgres v1.4.5
Expand Down Expand Up @@ -166,7 +168,6 @@ require (
golang.org/x/oauth2 v0.0.0-20221014153046-6fdb5e3db783 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.6.0 // indirect
golang.org/x/text v0.8.0 // indirect
golang.org/x/time v0.1.0 // indirect
golang.org/x/tools v0.7.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,8 @@ github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/alexedwards/scs/v2 v2.5.1 h1:EhAz3Kb3OSQzD8T+Ub23fKsiuvE0GzbF5Lgn0uTwM3Y=
github.com/alexedwards/scs/v2 v2.5.1/go.mod h1:ToaROZxyKukJKT/xLcVQAChi5k6+Pn1Gvmdl7h3RRj8=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ=
github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
Expand Down
9 changes: 7 additions & 2 deletions pkg/mycarehub/presentation/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/alexedwards/scs/v2"
"github.com/gorilla/handlers"
"github.com/gorilla/mux"
"github.com/savannahghi/firebasetools"
Expand Down Expand Up @@ -61,9 +62,13 @@ func Router(ctx context.Context) (*mux.Router, error) {
return nil, err
}

internalHandlers := internalRest.NewMyCareHubHandlersInterfaces(*useCases)
sessionManager := scs.New()
sessionManager.Lifetime = 24 * time.Hour

internalHandlers := internalRest.NewMyCareHubHandlersInterfaces(*useCases, sessionManager)

r := mux.NewRouter() // gorilla mux

r.Use(
handlers.RecoveryHandler(
handlers.PrintRecoveryStack(true),
Expand All @@ -81,7 +86,7 @@ func Router(ctx context.Context) (*mux.Router, error) {
http.MethodOptions,
http.MethodGet,
http.MethodPost,
).HandlerFunc(internalHandlers.AuthorizeHandler())
).Handler(sessionManager.LoadAndSave(internalHandlers.AuthorizeHandler()))

oauth2Routes.Path("/token").Methods(
http.MethodOptions,
Expand Down
38 changes: 34 additions & 4 deletions pkg/mycarehub/presentation/rest/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,45 @@ type okResp struct {
Status bool `json:"status"`
}

// SessionManager defines the methods for http session management

type SessionManager interface {
Put(ctx context.Context, key string, val interface{})
Destroy(ctx context.Context) error
Exists(ctx context.Context, key string) bool
GetBytes(ctx context.Context, key string) []byte
}

// OAuth2Provider is a consumer interface that contains the methods we need from fosite.OAuth2Provider interface
type OAuth2Provider interface {
NewAuthorizeRequest(ctx context.Context, req *http.Request) (fosite.AuthorizeRequester, error)
NewAuthorizeResponse(ctx context.Context, requester fosite.AuthorizeRequester, session fosite.Session) (fosite.AuthorizeResponder, error)
WriteAuthorizeError(ctx context.Context, rw http.ResponseWriter, requester fosite.AuthorizeRequester, err error)
WriteAuthorizeResponse(ctx context.Context, rw http.ResponseWriter, requester fosite.AuthorizeRequester, responder fosite.AuthorizeResponder)

NewAccessRequest(ctx context.Context, req *http.Request, session fosite.Session) (fosite.AccessRequester, error)
NewAccessResponse(ctx context.Context, requester fosite.AccessRequester) (fosite.AccessResponder, error)
WriteAccessResponse(ctx context.Context, rw http.ResponseWriter, requester fosite.AccessRequester, responder fosite.AccessResponder)
WriteAccessError(ctx context.Context, rw http.ResponseWriter, requester fosite.AccessRequester, err error)

NewRevocationRequest(ctx context.Context, r *http.Request) error
WriteRevocationResponse(ctx context.Context, rw http.ResponseWriter, err error)

NewIntrospectionRequest(ctx context.Context, r *http.Request, session fosite.Session) (fosite.IntrospectionResponder, error)
WriteIntrospectionResponse(ctx context.Context, rw http.ResponseWriter, r fosite.IntrospectionResponder)
WriteIntrospectionError(ctx context.Context, rw http.ResponseWriter, err error)
}

// MyCareHubHandlersInterfacesImpl represents the usecase implementation object
type MyCareHubHandlersInterfacesImpl struct {
provider fosite.OAuth2Provider
usecase usecases.MyCareHub
provider OAuth2Provider
usecase usecases.MyCareHub
sessionManager SessionManager
}

// NewMyCareHubHandlersInterfaces initializes a new rest handlers usecase
func NewMyCareHubHandlersInterfaces(usecase usecases.MyCareHub) MyCareHubHandlersInterfaces {
return &MyCareHubHandlersInterfacesImpl{usecase.Oauth.FositeProvider(), usecase}
func NewMyCareHubHandlersInterfaces(usecase usecases.MyCareHub, sessionManager SessionManager) MyCareHubHandlersInterfaces {
return &MyCareHubHandlersInterfacesImpl{usecase.Oauth.FositeProvider(), usecase, sessionManager}
}

// LoginByPhone is an unauthenticated endpoint that gets the phonenumber and pin
Expand Down
18 changes: 18 additions & 0 deletions pkg/mycarehub/presentation/rest/html/facility_chooser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{{define "content"}}
<div>
<h1 class="title">Please choose your Facility</h1>

<div class="program-box" >
{{range .AvailableFacilities}}
<div class="program-item" >
<form method="post">
<input name="facility" value={{.ID}} class="inv-iput">
<p class="subtitle">{{.Name}}</p>
<p>{{.Description}}</p>
<input type="submit" value="Select">
</form>
</div>
{{end}}
</div>
</div>
{{end}}
4 changes: 4 additions & 0 deletions pkg/mycarehub/presentation/rest/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func ServeLoginPage(w io.Writer, p LoginParams) error {
type ProgramChooserParams struct {
Title string
AvailablePrograms []*domain.Program
HasError bool
ErrorMessage string
}

func ServeProgramChooserPage(w io.Writer, p ProgramChooserParams) error {
Expand All @@ -40,6 +42,8 @@ func ServeProgramChooserPage(w io.Writer, p ProgramChooserParams) error {
type FacilityChooserParams struct {
Title string
AvailableFacilities []*domain.Facility
HasError bool
ErrorMessage string
}

func ServeFacilityChooserPage(w io.Writer, p FacilityChooserParams) error {
Expand Down
86 changes: 74 additions & 12 deletions pkg/mycarehub/presentation/rest/html/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
margin: 0;
padding: 0;
height: 100vh;
background-color: #9269ad;
background: #9269ad;
background-color: #9269AD;
background: #9269AD;
background-size: cover;
background-position: center;
font-family: "Sora", sans-serif;
font-family: 'Sora', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
}

.title {
display: flex;
align-items: center;
Expand All @@ -34,7 +34,7 @@
font-weight: 600;
color: white;
}

.login-box {
width: 320px;
height: 420px;
Expand All @@ -52,28 +52,29 @@
box-sizing: border-box;
padding: 70px 30px;
}

h2 {
margin: 0;
padding: 0 0 20px;
text-align: center;
font-size: 22px;

}

.login-box p {
margin: 0;
padding: 0;
color: #666666;
margin-bottom: 1px;
}

.login-box input {
width: 100%;
margin-bottom: 20px;
border-radius: 4px;
padding: 4px;
}

.login-box input[type="text"],
input[type="password"] {
border: 1px solid #666666;
Expand All @@ -82,18 +83,79 @@
height: 40px;
font-size: 18px;
}

.login-box input[type="submit"] {
border: none;
outline: none;
margin: 0;
height: 40px;
background: #9269ad;
background: #9269AD;
color: white;
font-size: 18px;
cursor: pointer;
}


.program-box {
display: flex;
flex-direction: column;
align-items: center;
background: white;
color: black;
border-radius: 8px;
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
top: 50%;
left: 50%;
position: absolute;
transform: translate(-50%, -50%);
box-sizing: border-box;
padding: 70px 30px;
}

.program-item {
padding: 20px;
background: #EFE9F3;
border-radius: 8px;
border: 1px solid #9269AD;
max-width: 420px;
min-width: 300px;
margin-top: 10px;
}

.program-item h4 {
color: #9269AD;
font-size: 18;
font-weight: 600;
}

.program-item .subtitle {
color: #B79CC9;
font-size: 18;
font-weight: 400;
}

p {
color: #697489;
font-size: 18;
font-weight: 400;
}

.program-item input[type="submit"] {
border: none;
outline: none;
margin: 0;
height: 40px;
background: #9269AD;
color: white;
font-size: 18px;
cursor: pointer;
width: 100%;
margin-bottom: 20px;
border-radius: 4px;
padding: 4px;
}
.inv-iput{
visibility: hidden;
}
</style>
<title>{{.Title}}</title>
</head>
Expand Down
19 changes: 19 additions & 0 deletions pkg/mycarehub/presentation/rest/html/program_chooser.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{{define "content"}}
<div>
<h1 class="title">Please choose your program</h1>

<div class="program-box" >
{{range .AvailablePrograms}}
<div class="program-item" >
<form method="post">
<input name="program" value={{.ID}} class="inv-iput">
<h4>{{.Organisation.Name}}</h4>
<p class="subtitle">{{.Name}}</p>
<p>{{.Description}}</p>
<input type="submit" value="Select">
</form>
</div>
{{end}}
</div>
</div>
{{end}}

0 comments on commit 6d5eca0

Please sign in to comment.