<commit_before>// Tideland Go REST Server Library - JSON Web Token - Header
//
// Copyright (C) 2016 <NAME> / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package jwt

//--------------------
// IMPORTS
//--------------------

import (
	"net/http"
	"strings"

	"github.com/tideland/gorest/rest"
)

//--------------------
// REQUEST HANDLING
//--------------------

// TokenFromJob retrieves a possible JWT from the request
// inside a REST job. The JWT is only decoded.
func TokenFromJob(job rest.Job) (JWT, error) {
	return TokenFromRequest(job.Request())
}

// TokenFromRequest retrieves a possible JWT from a
// HTTP request. The JWT is only decoded.
func TokenFromRequest(req *http.Request) (JWT, error) {
	authorization := req.Header.Get("Authorization")
	if authorization == "" {
		return nil, nil
	}
	fields := strings.Fields(authorization)
	if len(fields) != 2 || fields[0] != "Bearer" {
		return nil, nil
	}
	return Decode(fields[1])
}

// VerifiedTokenFromJob retrieves a possible JWT from 
// the request inside a REST job. The JWT is verified.
func VerifiedTokenFromJob(job rest.Job, key Key) (JWT, error) {
	return VerifiedTokenFromRequest(job.Request(), key)
}

// VerifiedTokenFromRequest retrieves a possible JWT from a
// HTTP request. The JWT is verified.
func VerifiedTokenFromRequest(req *http.Request, key Key) (JWT, error) {
	authorization := req.Header.Get("Authorization")
	if authorization == "" {
		return nil, nil
	}
	fields := strings.Fields(authorization)
	if len(fields) != 2 || fields[0] != "Bearer" {
		return nil, nil
	}
	return Verify(fields[1], key)
}


// EOF<commit_msg>Now can add token as header to request<commit_after>// Tideland Go REST Server Library - JSON Web Token - Header
//
// Copyright (C) 2016 <NAME> / Tideland / Oldenburg / Germany
//
// All rights reserved. Use of this source code is governed
// by the new BSD license.

package jwt

//--------------------
// IMPORTS
//--------------------

import (
	"net/http"
	"strings"

	"github.com/tideland/gorest/rest"
)

//--------------------
// REQUEST HANDLING
//--------------------

// TokenFromJob retrieves a possible JWT from the request
// inside a REST job. The JWT is only decoded.
func TokenFromJob(job rest.Job) (JWT, error) {
	return TokenFromRequest(job.Request())
}

// TokenFromRequest retrieves a possible JWT from a
// HTTP request. The JWT is only decoded.
func TokenFromRequest(req *http.Request) (JWT, error) {
	authorization := req.Header.Get("Authorization")
	if authorization == "" {
		return nil, nil
	}
	fields := strings.Fields(authorization)
	if len(fields) != 2 || fields[0] != "Bearer" {
		return nil, nil
	}
	return Decode(fields[1])
}

// VerifiedTokenFromJob retrieves a possible JWT from 
// the request inside a REST job. The JWT is verified.
func VerifiedTokenFromJob(job rest.Job, key Key) (JWT, error) {
	return VerifiedTokenFromRequest(job.Request(), key)
}

// VerifiedTokenFromRequest retrieves a possible JWT from a
// HTTP request. The JWT is verified.
func VerifiedTokenFromRequest(req *http.Request, key Key) (JWT, error) {
	authorization := req.Header.Get("Authorization")
	if authorization == "" {
		return nil, nil
	}
	fields := strings.Fields(authorization)
	if len(fields) != 2 || fields[0] != "Bearer" {
		return nil, nil
	}
	return Verify(fields[1], key)
}

// AddTokenToRequest adds a token as header to a request for
// usage by a client.
func AddTokenToRequest(req *http.Request, jwt JWT) *http.Request {
	req.Header.Add("Authorization", "Bearer " + jwt.String())
	return req
}

// EOF
{'fraction_non_alphanumeric': 0.1640841173215274, 'fraction_numerical': 0.005534034311012728, 'mean_word_length': 3.0663667041619798, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3338653', 'n_tokens_mistral': 1178, 'n_tokens_neox': 1138, 'n_words': 371}