2019-07-19 07:39:53 +00:00
|
|
|
package internal
|
2019-07-18 14:10:43 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// newToken returns new generated token with specified length
|
2019-07-20 16:33:02 +00:00
|
|
|
func NewToken(size int) string {
|
2019-07-18 14:10:43 +00:00
|
|
|
b := make([]byte, size)
|
|
|
|
rand.Read(b)
|
|
|
|
|
|
|
|
return fmt.Sprintf("%x", b)
|
|
|
|
}
|
|
|
|
|
|
|
|
// getTokenFromResponse returns token from request.
|
|
|
|
func getTokenFromResponse(r *http.Request) string {
|
|
|
|
const prefix = "Bearer "
|
|
|
|
|
|
|
|
auth, ok := r.Header["Authorization"]
|
|
|
|
if !ok {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, v := range auth {
|
|
|
|
if strings.HasPrefix(v, prefix) {
|
|
|
|
return strings.TrimPrefix(v, prefix)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|