2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2015-09-09 21:05:52 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
2015-09-30 01:21:17 +00:00
|
|
|
"fmt"
|
2015-09-09 21:05:52 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-10-04 13:35:47 +00:00
|
|
|
"github.com/golang-jwt/jwt/v4"
|
2021-10-12 07:25:13 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2015-09-09 21:05:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SecretFunc func(*Token) (string, error)
|
|
|
|
|
|
|
|
const (
|
2016-04-22 00:10:19 +00:00
|
|
|
UserToken = "user"
|
|
|
|
SessToken = "sess"
|
|
|
|
HookToken = "hook"
|
|
|
|
CsrfToken = "csrf"
|
|
|
|
AgentToken = "agent"
|
2015-09-09 21:05:52 +00:00
|
|
|
)
|
|
|
|
|
2021-09-27 21:32:08 +00:00
|
|
|
// SignerAlgo id default algorithm used to sign JWT tokens.
|
2015-09-09 21:05:52 +00:00
|
|
|
const SignerAlgo = "HS256"
|
|
|
|
|
|
|
|
type Token struct {
|
|
|
|
Kind string
|
|
|
|
Text string
|
|
|
|
}
|
|
|
|
|
2021-09-27 21:32:08 +00:00
|
|
|
func parse(raw string, fn SecretFunc) (*Token, error) {
|
2015-09-09 21:05:52 +00:00
|
|
|
token := &Token{}
|
|
|
|
parsed, err := jwt.Parse(raw, keyFunc(token, fn))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !parsed.Valid {
|
|
|
|
return nil, jwt.ValidationError{}
|
|
|
|
}
|
|
|
|
return token, nil
|
|
|
|
}
|
|
|
|
|
2015-09-30 01:21:17 +00:00
|
|
|
func ParseRequest(r *http.Request, fn SecretFunc) (*Token, error) {
|
2021-09-27 21:32:08 +00:00
|
|
|
token := r.Header.Get("Authorization")
|
2015-09-30 01:21:17 +00:00
|
|
|
|
|
|
|
// first we attempt to get the token from the
|
|
|
|
// authorization header.
|
|
|
|
if len(token) != 0 {
|
2021-10-12 07:25:13 +00:00
|
|
|
log.Trace().Msgf("token.ParseRequest: found token in header: %s", token)
|
2021-09-27 21:32:08 +00:00
|
|
|
bearer := token
|
|
|
|
if _, err := fmt.Sscanf(token, "Bearer %s", &bearer); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return parse(bearer, fn)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// then we attempt to get the token from the
|
|
|
|
// access_token url query parameter
|
|
|
|
token = r.FormValue("access_token")
|
|
|
|
if len(token) != 0 {
|
2021-09-27 21:32:08 +00:00
|
|
|
return parse(token, fn)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
2016-03-27 00:14:00 +00:00
|
|
|
// and finally we attempt to get the token from
|
2015-09-30 01:21:17 +00:00
|
|
|
// the user session cookie
|
|
|
|
cookie, err := r.Cookie("user_sess")
|
2015-09-09 21:05:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-09-27 21:32:08 +00:00
|
|
|
return parse(cookie.Value, fn)
|
2015-09-30 01:21:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CheckCsrf(r *http.Request, fn SecretFunc) error {
|
|
|
|
// get and options requests are always
|
|
|
|
// enabled, without CSRF checks.
|
|
|
|
switch r.Method {
|
|
|
|
case "GET", "OPTIONS":
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse the raw CSRF token value and validate
|
|
|
|
raw := r.Header.Get("X-CSRF-TOKEN")
|
2021-09-27 21:32:08 +00:00
|
|
|
_, err := parse(raw, fn)
|
2015-09-30 01:21:17 +00:00
|
|
|
return err
|
2015-09-09 21:05:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(kind, text string) *Token {
|
|
|
|
return &Token{Kind: kind, Text: text}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs the token using the given secret hash
|
|
|
|
// and returns the string value.
|
|
|
|
func (t *Token) Sign(secret string) (string, error) {
|
|
|
|
return t.SignExpires(secret, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sign signs the token using the given secret hash
|
|
|
|
// with an expiration date.
|
|
|
|
func (t *Token) SignExpires(secret string, exp int64) (string, error) {
|
|
|
|
token := jwt.New(jwt.SigningMethodHS256)
|
2021-09-21 10:55:25 +00:00
|
|
|
claims, ok := token.Claims.(jwt.MapClaims)
|
|
|
|
if !ok {
|
|
|
|
return "", fmt.Errorf("token claim is not a MapClaims")
|
|
|
|
}
|
|
|
|
claims["type"] = t.Kind
|
|
|
|
claims["text"] = t.Text
|
2015-09-09 21:05:52 +00:00
|
|
|
if exp > 0 {
|
2021-09-21 10:55:25 +00:00
|
|
|
claims["exp"] = float64(exp)
|
2015-09-09 21:05:52 +00:00
|
|
|
}
|
|
|
|
return token.SignedString([]byte(secret))
|
|
|
|
}
|
|
|
|
|
|
|
|
func keyFunc(token *Token, fn SecretFunc) jwt.Keyfunc {
|
|
|
|
return func(t *jwt.Token) (interface{}, error) {
|
2021-09-21 10:55:25 +00:00
|
|
|
claims, ok := t.Claims.(jwt.MapClaims)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("token claim is not a MapClaims")
|
|
|
|
}
|
|
|
|
|
2015-09-09 21:05:52 +00:00
|
|
|
// validate the correct algorithm is being used
|
|
|
|
if t.Method.Alg() != SignerAlgo {
|
|
|
|
return nil, jwt.ErrSignatureInvalid
|
|
|
|
}
|
|
|
|
|
|
|
|
// extract the token kind and cast to
|
|
|
|
// the expected type.
|
2021-09-21 10:55:25 +00:00
|
|
|
kindv, ok := claims["type"]
|
2015-09-09 21:05:52 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, jwt.ValidationError{}
|
|
|
|
}
|
|
|
|
token.Kind, _ = kindv.(string)
|
|
|
|
|
|
|
|
// extract the token value and cast to
|
2021-10-08 16:35:56 +00:00
|
|
|
// expected type.
|
2021-09-21 10:55:25 +00:00
|
|
|
textv, ok := claims["text"]
|
2015-09-09 21:05:52 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, jwt.ValidationError{}
|
|
|
|
}
|
|
|
|
token.Text, _ = textv.(string)
|
|
|
|
|
|
|
|
// invoke the callback function to retrieve
|
|
|
|
// the secret key used to verify
|
|
|
|
secret, err := fn(token)
|
|
|
|
return []byte(secret), err
|
|
|
|
}
|
|
|
|
}
|