...

Source file src/github.com/ory/fosite/handler/oauth2/strategy_hmacsha.go

Documentation: github.com/ory/fosite/handler/oauth2

     1  /*
     2   * Copyright © 2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   * @author		Aeneas Rekkas <aeneas+oss@aeneas.io>
    17   * @copyright 	2015-2018 Aeneas Rekkas <aeneas+oss@aeneas.io>
    18   * @license 	Apache-2.0
    19   *
    20   */
    21  
    22  package oauth2
    23  
    24  import (
    25  	"context"
    26  	"time"
    27  
    28  	"github.com/ory/x/errorsx"
    29  
    30  	"github.com/ory/fosite"
    31  	enigma "github.com/ory/fosite/token/hmac"
    32  )
    33  
    34  type HMACSHAStrategy struct {
    35  	Enigma                *enigma.HMACStrategy
    36  	AccessTokenLifespan   time.Duration
    37  	RefreshTokenLifespan  time.Duration
    38  	AuthorizeCodeLifespan time.Duration
    39  }
    40  
    41  func (h HMACSHAStrategy) AccessTokenSignature(token string) string {
    42  	return h.Enigma.Signature(token)
    43  }
    44  func (h HMACSHAStrategy) RefreshTokenSignature(token string) string {
    45  	return h.Enigma.Signature(token)
    46  }
    47  func (h HMACSHAStrategy) AuthorizeCodeSignature(token string) string {
    48  	return h.Enigma.Signature(token)
    49  }
    50  
    51  func (h HMACSHAStrategy) GenerateAccessToken(_ context.Context, _ fosite.Requester) (token string, signature string, err error) {
    52  	return h.Enigma.Generate()
    53  }
    54  
    55  func (h HMACSHAStrategy) ValidateAccessToken(_ context.Context, r fosite.Requester, token string) (err error) {
    56  	var exp = r.GetSession().GetExpiresAt(fosite.AccessToken)
    57  	if exp.IsZero() && r.GetRequestedAt().Add(h.AccessTokenLifespan).Before(time.Now().UTC()) {
    58  		return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Access token expired at '%s'.", r.GetRequestedAt().Add(h.AccessTokenLifespan)))
    59  	}
    60  	if !exp.IsZero() && exp.Before(time.Now().UTC()) {
    61  		return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Access token expired at '%s'.", exp))
    62  	}
    63  	return h.Enigma.Validate(token)
    64  }
    65  
    66  func (h HMACSHAStrategy) GenerateRefreshToken(_ context.Context, _ fosite.Requester) (token string, signature string, err error) {
    67  	return h.Enigma.Generate()
    68  }
    69  
    70  func (h HMACSHAStrategy) ValidateRefreshToken(_ context.Context, r fosite.Requester, token string) (err error) {
    71  	var exp = r.GetSession().GetExpiresAt(fosite.RefreshToken)
    72  	if exp.IsZero() {
    73  		// Unlimited lifetime
    74  		return h.Enigma.Validate(token)
    75  	}
    76  	if !exp.IsZero() && exp.Before(time.Now().UTC()) {
    77  		return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Refresh token expired at '%s'.", exp))
    78  	}
    79  	return h.Enigma.Validate(token)
    80  }
    81  
    82  func (h HMACSHAStrategy) GenerateAuthorizeCode(_ context.Context, _ fosite.Requester) (token string, signature string, err error) {
    83  	return h.Enigma.Generate()
    84  }
    85  
    86  func (h HMACSHAStrategy) ValidateAuthorizeCode(_ context.Context, r fosite.Requester, token string) (err error) {
    87  	var exp = r.GetSession().GetExpiresAt(fosite.AuthorizeCode)
    88  	if exp.IsZero() && r.GetRequestedAt().Add(h.AuthorizeCodeLifespan).Before(time.Now().UTC()) {
    89  		return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Authorize code expired at '%s'.", r.GetRequestedAt().Add(h.AuthorizeCodeLifespan)))
    90  	}
    91  	if !exp.IsZero() && exp.Before(time.Now().UTC()) {
    92  		return errorsx.WithStack(fosite.ErrTokenExpired.WithHintf("Authorize code expired at '%s'.", exp))
    93  	}
    94  
    95  	return h.Enigma.Validate(token)
    96  }
    97  

View as plain text