Skip to content

Commit db7403f

Browse files
arnold-iakabArnold Iakab
and
Arnold Iakab
authored
Extend oauth2 token with auth time field (#363)
Co-authored-by: Arnold Iakab <[email protected]>
1 parent 0923028 commit db7403f

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

http/oauth2/introspection.go

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ type IntrospectResponse struct {
2929
Scope string `json:"scope"`
3030
ClientID string `json:"client_id"`
3131
UserID string `json:"user_id"`
32+
AuthTime int64 `json:"auth_time"`
3233

3334
// Backend identifies the backend used for introspection. This attribute
3435
// exists as a convenience if you have more than one authorization backend

http/oauth2/oauth2.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,9 @@ package oauth2
99
import (
1010
"context"
1111
"errors"
12-
"net/http"
13-
1412
"github.com/opentracing/opentracing-go"
1513
olog "github.com/opentracing/opentracing-go/log"
14+
"net/http"
1615

1716
"github.com/pace/bricks/http/security"
1817
"github.com/pace/bricks/maintenance/log"
@@ -46,6 +45,7 @@ type token struct {
4645
value string
4746
userID string
4847
clientID string
48+
authTime int64
4949
scope Scope
5050
backend interface{}
5151
}
@@ -102,6 +102,7 @@ func fromIntrospectResponse(s *IntrospectResponse, tokenValue string) token {
102102
t := token{
103103
userID: s.UserID,
104104
value: tokenValue,
105+
authTime: s.AuthTime,
105106
clientID: s.ClientID,
106107
backend: s.Backend,
107108
}
@@ -141,6 +142,16 @@ func UserID(ctx context.Context) (string, bool) {
141142
return oauth2token.userID, true
142143
}
143144

145+
// AuthTime returns the auth time stored in ctx as unix timestamp
146+
func AuthTime(ctx context.Context) (int64, bool) {
147+
tok, _ := security.GetTokenFromContext(ctx)
148+
oauth2token, ok := tok.(*token)
149+
if !ok {
150+
return 0, false
151+
}
152+
return oauth2token.authTime, true
153+
}
154+
144155
// Scopes returns the scopes stored in ctx
145156
func Scopes(ctx context.Context) []string {
146157
tok, _ := security.GetTokenFromContext(ctx)

0 commit comments

Comments
 (0)