Skip to content

Commit

Permalink
Merge pull request #114 from vkareh/ocm-sdk-token-exp
Browse files Browse the repository at this point in the history
Use OCM SDK to get token expiration
  • Loading branch information
openshift-merge-robot authored Sep 22, 2020
2 parents ecb9240 + dba0978 commit 224e7cf
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 30 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/mitchellh/go-homedir v1.1.0
github.com/onsi/ginkgo v1.11.0
github.com/onsi/gomega v1.7.0
github.com/openshift-online/ocm-sdk-go v0.1.128
github.com/openshift-online/ocm-sdk-go v0.1.130
github.com/prometheus/common v0.11.1 // indirect
github.com/sirupsen/logrus v1.6.0
github.com/spf13/cobra v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/op/go-logging v0.0.0-20160315200505-970db520ece7/go.mod h1:HzydrMdWErDVzsI23lYNej1Htcns9BCg93Dk0bBINWk=
github.com/openshift-online/ocm-sdk-go v0.1.128 h1:AlX7PkZOuZJPBzMM2CgF22YGMp6M9+UUPp8bfGORTDM=
github.com/openshift-online/ocm-sdk-go v0.1.128/go.mod h1:3i3a/LEYtC7DMor3N94PTPn1+0qq+Fk+iLjt1Z0WVCs=
github.com/openshift-online/ocm-sdk-go v0.1.130 h1:2hnPXJSgkt0LSSNzO2lvJuFBq3rqZ01ntTMUua0D5fo=
github.com/openshift-online/ocm-sdk-go v0.1.130/go.mod h1:3i3a/LEYtC7DMor3N94PTPn1+0qq+Fk+iLjt1Z0WVCs=
github.com/opentracing-contrib/go-observer v0.0.0-20170622124052-a52f23424492/go.mod h1:Ngi6UdF0k5OKD5t5wlmGhe/EDKPoUM3BXZSSfIuJbis=
github.com/opentracing/basictracer-go v1.0.0/go.mod h1:QfBfYuafItcjQuMwinw9GhYKwFXS9KnPs5lxoYwgW74=
github.com/opentracing/opentracing-go v1.0.2/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Expand Down
42 changes: 15 additions & 27 deletions pkg/ocm/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ func (c *Config) Armed() (armed bool, err error) {
if c.AccessToken != "" {
var expires bool
var left time.Duration
expires, left, err = tokenExpiry(c.AccessToken, now)
var accessToken *jwt.Token
accessToken, err = parseToken(c.AccessToken)
if err != nil {
return
}
expires, left, err = sdk.GetTokenExpiry(accessToken, now)
if err != nil {
return
}
Expand All @@ -187,7 +192,12 @@ func (c *Config) Armed() (armed bool, err error) {
if c.RefreshToken != "" {
var expires bool
var left time.Duration
expires, left, err = tokenExpiry(c.RefreshToken, now)
var refreshToken *jwt.Token
refreshToken, err = parseToken(c.RefreshToken)
if err != nil {
return
}
expires, left, err = sdk.GetTokenExpiry(refreshToken, now)
if err != nil {
return
}
Expand Down Expand Up @@ -252,34 +262,12 @@ func (c *Config) Connection() (connection *sdk.Connection, err error) {
return
}

func tokenExpiry(text string, now time.Time) (expires bool, left time.Duration, err error) {
func parseToken(textToken string) (token *jwt.Token, err error) {
parser := new(jwt.Parser)
token, _, err := parser.ParseUnverified(text, jwt.MapClaims{})
token, _, err = parser.ParseUnverified(textToken, jwt.MapClaims{})
if err != nil {
err = fmt.Errorf("Failed to parse token: %v", err)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
err = fmt.Errorf("Expected map claims but got %T", claims)
return
}
claim, ok := claims["exp"]
if !ok {
err = fmt.Errorf("Token does not contain the 'exp' claim")
return
}
exp, ok := claim.(float64)
if !ok {
err = fmt.Errorf("Expected floating point 'exp' but got %T", claim)
return
}
if exp == 0 {
expires = false
left = 0
} else {
expires = true
left = time.Unix(int64(exp), 0).Sub(now)
}
return
return token, nil
}

0 comments on commit 224e7cf

Please sign in to comment.