Skip to content

Commit 283dc29

Browse files
author
Arnold Iakab
committed
Extend oauth2 token with auth time field
1 parent 0923028 commit 283dc29

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

http/oauth2/introspection.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package oauth2
66
import (
77
"context"
88
"errors"
9+
"time"
910
)
1011

1112
// TokenIntrospecter needs to be implemented for token lookup
@@ -25,10 +26,11 @@ var ErrBadUpstreamResponse = errors.New("bad upstream response when introspectin
2526
// IntrospectResponse in case of a successful check of the
2627
// oauth2 request
2728
type IntrospectResponse struct {
28-
Active bool `json:"active"`
29-
Scope string `json:"scope"`
30-
ClientID string `json:"client_id"`
31-
UserID string `json:"user_id"`
29+
Active bool `json:"active"`
30+
Scope string `json:"scope"`
31+
ClientID string `json:"client_id"`
32+
UserID string `json:"user_id"`
33+
AuthTime time.Time `json:"auth_time"`
3234

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

http/oauth2/oauth2.go

+13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"context"
1111
"errors"
1212
"net/http"
13+
"time"
1314

1415
"github.com/opentracing/opentracing-go"
1516
olog "github.com/opentracing/opentracing-go/log"
@@ -46,6 +47,7 @@ type token struct {
4647
value string
4748
userID string
4849
clientID string
50+
authTime time.Time
4951
scope Scope
5052
backend interface{}
5153
}
@@ -102,6 +104,7 @@ func fromIntrospectResponse(s *IntrospectResponse, tokenValue string) token {
102104
t := token{
103105
userID: s.UserID,
104106
value: tokenValue,
107+
authTime: s.AuthTime,
105108
clientID: s.ClientID,
106109
backend: s.Backend,
107110
}
@@ -141,6 +144,16 @@ func UserID(ctx context.Context) (string, bool) {
141144
return oauth2token.userID, true
142145
}
143146

147+
// AuthTime returns the auth time stored in ctx
148+
func AuthTime(ctx context.Context) (time.Time, bool) {
149+
tok, _ := security.GetTokenFromContext(ctx)
150+
oauth2token, ok := tok.(*token)
151+
if !ok {
152+
return time.Time{}, false
153+
}
154+
return oauth2token.authTime, true
155+
}
156+
144157
// Scopes returns the scopes stored in ctx
145158
func Scopes(ctx context.Context) []string {
146159
tok, _ := security.GetTokenFromContext(ctx)

0 commit comments

Comments
 (0)