From 68eb1b446cc30b1ca6c4eff285dbbc60eb08d79a Mon Sep 17 00:00:00 2001
From: Christian Pointner <equinox@helsinki.at>
Date: Sun, 21 Apr 2019 15:23:57 +0200
Subject: [PATCH] oidc session creation with existing access_token works now

---
 auth/oidc.go     | 22 ++++++++++++++++++++--
 auth/sessions.go | 20 ++++++++++++--------
 2 files changed, 32 insertions(+), 10 deletions(-)

diff --git a/auth/oidc.go b/auth/oidc.go
index 62664c2..e0159d7 100644
--- a/auth/oidc.go
+++ b/auth/oidc.go
@@ -51,7 +51,12 @@ func (s *OIDCSession) refresh(ctx context.Context) (*Session, error) {
 		return nil, errors.New("fetching OIDC UserInfo failed: " + err.Error())
 	}
 
-	newS := &Session{}
+	// TOOD: if we later use sessions.update() it is overkill generate
+	//       a whole new session...
+	newS, err := NewSession()
+	if err != nil {
+		return nil, err
+	}
 	if err := userInfo.Claims(newS); err != nil {
 		return nil, errors.New("parsing OIDC UserInfo failed: " + err.Error())
 	}
@@ -59,6 +64,19 @@ func (s *OIDCSession) refresh(ctx context.Context) (*Session, error) {
 	return newS, nil
 }
 
+// This is only safe when session is logged in!
+func (s *OIDCSession) MarshalJSON() ([]byte, error) {
+	t, err := s.tokenSource.Token()
+	if err != nil {
+		return nil, err
+	}
+	return json.Marshal(struct {
+		Token *oauth2.Token `json:"token,omitempty"`
+	}{
+		Token: t,
+	})
+}
+
 type OIDCBackend struct {
 	loginTimeout time.Duration
 	issuerURL    string
@@ -115,7 +133,7 @@ func (b *OIDCBackend) NewOIDCSession(ctx context.Context, arguments json.RawMess
 		}
 
 		os.tokenSource = oauth2.StaticTokenSource(&oauth2Token)
-		if s, err = s.oidc.refresh(ctx); err != nil {
+		if s, err = os.refresh(ctx); err != nil {
 			return
 		}
 		s.setState(SessionStateLoggedIn)
diff --git a/auth/sessions.go b/auth/sessions.go
index d85663a..c4c0db0 100644
--- a/auth/sessions.go
+++ b/auth/sessions.go
@@ -175,27 +175,31 @@ func (s *Session) cleanup() {
 }
 
 func (s *Session) MarshalJSON() ([]byte, error) {
-	var expires *time.Time
-	if deadline, ok := s.ctx.Deadline(); ok {
-		expires = &deadline
-	}
-	return json.Marshal(struct {
+	data := struct {
 		ID       string       `json:"id"`
 		State    SessionState `json:"state"`
 		Expires  *time.Time   `json:"expires,omitempty"`
 		Username string       `json:"username"`
 		ReadOnly bool         `json:"readonly"`
 		AllShows bool         `json:"all-shows"`
-		Shows    []string     `json:"shows"`
+		Shows    []string     `json:"shows,omitempty"`
+		OIDC     *OIDCSession `json:"oidc,omitempty"`
 	}{
 		ID:       s.id,
 		State:    s.State(),
-		Expires:  expires,
 		Username: s.Username,
 		ReadOnly: s.ReadOnly,
 		AllShows: s.AllShows,
 		Shows:    s.Shows,
-	})
+	}
+	if deadline, ok := s.ctx.Deadline(); ok {
+		data.Expires = &deadline
+	}
+	// this should only be enabled for debug purposes
+	// if data.State == SessionStateLoggedIn {
+	// 	data.OIDC = s.oidc
+	// }
+	return json.Marshal(data)
 }
 
 func (s *Session) getToken() string {
-- 
GitLab