Skip to content
Snippets Groups Projects
Commit 483a7810 authored by Gabriel Zachmann's avatar Gabriel Zachmann
Browse files

add request metadata to events

parent 4cc71167
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,8 @@ import ( ...@@ -6,6 +6,8 @@ import (
"log" "log"
"time" "time"
"github.com/zachmann/mytoken/internal/model"
"github.com/go-sql-driver/mysql" "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx" "github.com/jmoiron/sqlx"
...@@ -34,15 +36,17 @@ type SuperTokenEntry struct { ...@@ -34,15 +36,17 @@ type SuperTokenEntry struct {
Name string Name string
CreatedAt time.Time `db:"created_at"` CreatedAt time.Time `db:"created_at"`
IP string `db:"ip_created"` IP string `db:"ip_created"`
networkData model.NetworkData
} }
func NewSuperTokenEntry(name, oidcSub, oidcIss string, r restrictions.Restrictions, c capabilities.Capabilities, ip string) *SuperTokenEntry { func NewSuperTokenEntry(name, oidcSub, oidcIss string, r restrictions.Restrictions, c capabilities.Capabilities, networkData model.NetworkData) *SuperTokenEntry {
st := supertoken.NewSuperToken(oidcSub, oidcIss, r, c) st := supertoken.NewSuperToken(oidcSub, oidcIss, r, c)
return &SuperTokenEntry{ return &SuperTokenEntry{
ID: st.ID, ID: st.ID,
Token: st, Token: st,
Name: name, Name: name,
IP: ip, IP: networkData.IP,
networkData: networkData,
} }
} }
...@@ -70,7 +74,7 @@ func (ste *SuperTokenEntry) Store(comment string) error { ...@@ -70,7 +74,7 @@ func (ste *SuperTokenEntry) Store(comment string) error {
if err != nil { if err != nil {
return err return err
} }
return eventService.LogEvent(*event.FromNumber(event.STEventSTCreated, comment), ste.ID) return eventService.LogEvent(*event.FromNumber(event.STEventSTCreated, comment), ste.ID, ste.networkData)
} }
type superTokenEntryStore struct { type superTokenEntryStore struct {
......
...@@ -28,6 +28,10 @@ func HandleOIDCRedirect(ctx *fiber.Ctx) error { ...@@ -28,6 +28,10 @@ func HandleOIDCRedirect(ctx *fiber.Ctx) error {
return errorRes.Send(ctx) return errorRes.Send(ctx)
} }
code := ctx.Query("code") code := ctx.Query("code")
res := authcode.CodeExchange(state, code, ctx.IP()) networkData := model.NetworkData{
IP: ctx.IP(),
UserAgent: string(ctx.Request().Header.UserAgent()),
}
res := authcode.CodeExchange(state, code, networkData)
return res.Send(ctx) return res.Send(ctx)
} }
package model
type NetworkData struct {
IP string
UserAgent string
}
...@@ -106,7 +106,7 @@ func InitAuthCodeFlow(provider *config.ProviderConf, req *response.AuthCodeFlowR ...@@ -106,7 +106,7 @@ func InitAuthCodeFlow(provider *config.ProviderConf, req *response.AuthCodeFlowR
return return
} }
func CodeExchange(state, code, ip string) model.Response { func CodeExchange(state, code string, networkData model.NetworkData) model.Response {
log.Print("Handle code exchange") log.Print("Handle code exchange")
authInfo, err := dbModels.GetAuthCodeInfoByState(state) authInfo, err := dbModels.GetAuthCodeInfoByState(state)
if err != nil { if err != nil {
...@@ -152,13 +152,13 @@ func CodeExchange(state, code, ip string) model.Response { ...@@ -152,13 +152,13 @@ func CodeExchange(state, code, ip string) model.Response {
if err != nil { if err != nil {
return model.ErrorToInternalServerErrorResponse(err) return model.ErrorToInternalServerErrorResponse(err)
} }
ste, err := createSuperTokenEntry(authInfo, token, oidcSub, ip) ste, err := createSuperTokenEntry(authInfo, token, oidcSub, networkData)
if err != nil { if err != nil {
return model.ErrorToInternalServerErrorResponse(err) return model.ErrorToInternalServerErrorResponse(err)
} }
at := dbModels.AccessToken{ at := dbModels.AccessToken{
Token: token.AccessToken, Token: token.AccessToken,
IP: ip, IP: networkData.IP,
Comment: "Initial Access Token from authorization code flow", Comment: "Initial Access Token from authorization code flow",
STID: ste.ID, STID: ste.ID,
Scopes: nil, //TODO Scopes: nil, //TODO
...@@ -191,8 +191,8 @@ func CodeExchange(state, code, ip string) model.Response { ...@@ -191,8 +191,8 @@ func CodeExchange(state, code, ip string) model.Response {
} }
} }
func createSuperTokenEntry(authFlowInfo *dbModels.AuthFlowInfo, token *oauth2.Token, oidcSub, ip string) (*dbModels.SuperTokenEntry, error) { func createSuperTokenEntry(authFlowInfo *dbModels.AuthFlowInfo, token *oauth2.Token, oidcSub string, networkData model.NetworkData) (*dbModels.SuperTokenEntry, error) {
ste := dbModels.NewSuperTokenEntry(authFlowInfo.Name, oidcSub, authFlowInfo.Issuer, authFlowInfo.Restrictions, authFlowInfo.Capabilities, ip) ste := dbModels.NewSuperTokenEntry(authFlowInfo.Name, oidcSub, authFlowInfo.Issuer, authFlowInfo.Restrictions, authFlowInfo.Capabilities, networkData)
ste.RefreshToken = token.RefreshToken ste.RefreshToken = token.RefreshToken
err := ste.Store("Used grant_type oidc_flow authorization_code") err := ste.Store("Used grant_type oidc_flow authorization_code")
if err != nil { if err != nil {
......
...@@ -3,16 +3,13 @@ package event ...@@ -3,16 +3,13 @@ package event
import ( import (
uuid "github.com/satori/go.uuid" uuid "github.com/satori/go.uuid"
"github.com/zachmann/mytoken/internal/db" "github.com/zachmann/mytoken/internal/db"
"github.com/zachmann/mytoken/internal/model"
pkg "github.com/zachmann/mytoken/internal/supertoken/event/pkg" pkg "github.com/zachmann/mytoken/internal/supertoken/event/pkg"
) )
func LogEvent(event pkg.Event, stid uuid.UUID) error { func LogEvent(event pkg.Event, stid uuid.UUID, metaData model.NetworkData) error {
//TODO
ip := "192.168.0.31"
userAgent := "go"
_, err := db.DB().Exec(`INSERT INTO ST_Events _, err := db.DB().Exec(`INSERT INTO ST_Events
(ST_id, event_id, comment, ip, user_agent) (ST_id, event_id, comment, ip, user_agent)
VALUES(?, (SELECT id FROM Events WHERE event=?), ?, ?, ?)`, stid, event.String(), event.Comment, ip, userAgent) VALUES(?, (SELECT id FROM Events WHERE event=?), ?, ?, ?)`, stid, event.String(), event.Comment, metaData.IP, metaData.UserAgent)
return err return err
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment