mirror of
https://github.com/signaller-matrix/signaller.git
synced 2024-11-22 02:12:20 +00:00
35 lines
561 B
Go
35 lines
561 B
Go
package internal
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
// newToken returns new generated token with specified length
|
|
func newToken(size int) string {
|
|
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 ""
|
|
}
|