Albirew/nyaa-pantsu
Archivé
1
0
Bifurcation 0
Ce dépôt a été archivé le 2022-05-07. Vous pouvez voir ses fichiers ou le cloner, mais pas ouvrir de ticket ou de demandes d'ajout, ni soumettre de changements.
nyaa-pantsu/vendor/github.com/ory/fosite/token/hmac/hmacsha.go
akuma06 03ea72595d OAuth API [done] (#1275)
* Initial Commit for OAuth API

This builds and run and return the right error.
Need to test it and then adding all users as possible client

* Added mising dependency

* just compile already...

* Fixing template test

* Imrpovements

Moved db stuff in models
Added some tests
Added form in modpanel to add/update a client
Added controllers for add/update of client

* Added Forms + speed improvements

Controller oauth client listing + html
Controller oauth client delete + messages
Messages on comment delete
New ES config that disable ES if set to false. Improve load speed on local development
Fix a load config bug
Fix index admin & translation string sign_out broken by @ewhal

* Sanitize empty strig in form array + css

Multiple empty array of strings are sanitized for the oauth client create form
Added some css for the form display

* Upload and Create form works

* Fix splitting response types

* Removing required on secret when updating

* fix travis error

* Fix travis template test

* Update dependency

* Moved to jinzhu instead of azhao

* randomizen secret on creation

* Final touch on oath api

improved display name
fix grant form csrf
fix login csrf on oauth

* Fix gorm test

* fix template test

* Fixing deleted dependency issue

* Make travis faster

* Fix typo

* Fix csrf for api calls

* This shouldn't be exempt

* Removing hard coded hash

@ewhal Don't forget to replace the hash in tokens.go with another one

* Added an example on how to use OAuth middleware

* Renamed fosite utils to oauth2 utils
2017-07-28 13:46:40 +10:00

118 lignes
3,2 Kio
Go

// Package hmac is the default implementation for generating and validating challenges. It uses HMAC-SHA256 to
// generate and validate challenges.
package hmac
import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"fmt"
"strings"
"github.com/ory/fosite"
"github.com/pkg/errors"
)
// HMACStrategy is responsible for generating and validating challenges.
type HMACStrategy struct {
AuthCodeEntropy int
GlobalSecret []byte
}
const (
// key should be at least 256 bit long, making it
minimumEntropy = 32
// the secrets (client and global) should each have at least 16 characters making it harder to guess them
minimumSecretLength = 32
)
var b64 = base64.URLEncoding.WithPadding(base64.NoPadding)
// Generate generates a token and a matching signature or returns an error.
// This method implements rfc6819 Section 5.1.4.2.2: Use High Entropy for Secrets.
func (c *HMACStrategy) Generate() (string, string, error) {
if len(c.GlobalSecret) < minimumSecretLength/2 {
return "", "", errors.New("Secret is not strong enough")
}
if c.AuthCodeEntropy < minimumEntropy {
c.AuthCodeEntropy = minimumEntropy
}
// When creating secrets not intended for usage by human users (e.g.,
// client secrets or token handles), the authorization server should
// include a reasonable level of entropy in order to mitigate the risk
// of guessing attacks. The token value should be >=128 bits long and
// constructed from a cryptographically strong random or pseudo-random
// number sequence (see [RFC4086] for best current practice) generated
// by the authorization server.
key, err := RandomBytes(c.AuthCodeEntropy)
if err != nil {
return "", "", errors.WithStack(err)
}
if len(key) < c.AuthCodeEntropy {
return "", "", errors.New("Could not read enough random data for key generation")
}
useSecret := append([]byte{}, c.GlobalSecret...)
mac := hmac.New(sha256.New, useSecret)
_, err = mac.Write(key)
if err != nil {
return "", "", errors.WithStack(err)
}
signature := mac.Sum([]byte{})
encodedSignature := b64.EncodeToString(signature)
encodedToken := fmt.Sprintf("%s.%s", b64.EncodeToString(key), encodedSignature)
return encodedToken, encodedSignature, nil
}
// Validate validates a token and returns its signature or an error if the token is not valid.
func (c *HMACStrategy) Validate(token string) error {
split := strings.Split(token, ".")
if len(split) != 2 {
return errors.WithStack(fosite.ErrInvalidTokenFormat)
}
key := split[0]
signature := split[1]
if key == "" || signature == "" {
return errors.WithStack(fosite.ErrInvalidTokenFormat)
}
decodedSignature, err := b64.DecodeString(signature)
if err != nil {
return errors.WithStack(err)
}
decodedKey, err := b64.DecodeString(key)
if err != nil {
return errors.WithStack(err)
}
useSecret := append([]byte{}, c.GlobalSecret...)
mac := hmac.New(sha256.New, useSecret)
_, err = mac.Write(decodedKey)
if err != nil {
return errors.WithStack(err)
}
if !hmac.Equal(decodedSignature, mac.Sum([]byte{})) {
// Hash is invalid
return errors.WithStack(fosite.ErrTokenSignatureMismatch)
}
return nil
}
func (c *HMACStrategy) Signature(token string) string {
split := strings.Split(token, ".")
if len(split) != 2 {
return ""
}
return split[1]
}