Skip to content
Snippets Groups Projects
Commit d2538d15 authored by Sergey Yakubov's avatar Sergey Yakubov
Browse files

started working at tokens

parent 5175f5bb
No related branches found
No related tags found
No related merge requests found
......@@ -14,9 +14,9 @@ import (
"testing"
)
func prepareToken(beamtime_or_beamline string) string{
func prepareToken(payload string) string{
authHMAC = utils.NewHMACAuth("secret")
token, _ := authHMAC.GenerateToken(&beamtime_or_beamline)
token, _ := authHMAC.GenerateToken(&payload)
return token
}
......@@ -52,6 +52,19 @@ func doPostRequest(path string,buf string) *httptest.ResponseRecorder {
return w
}
func doGetRequest(path string,token string) *httptest.ResponseRecorder {
mux := utils.NewRouter(listRoutes)
req, _ := http.NewRequest("GET", path, nil)
req.Header.Add("Authorization", authHMAC.Name() + token)
w := httptest.NewRecorder()
mux.ServeHTTP(w, req)
return w
}
var credTests = [] struct {
request string
cred SourceCredentials
......
package server
import (
"asapo_common/utils"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"strconv"
"testing"
"time"
)
var IssueTokenTests = [] struct {
beamtimeId string
beamline string
role string
validDays string
adminToken string
resToken string
status int
message string
}{
{"test", "","read","180",prepareToken("admin"),"aaa",http.StatusOK,"read for beamtime"},
{"test", "","read","180",prepareToken("admin"),"aaa",http.StatusOK,"write for beamtime"},
{"", "test","read","180",prepareToken("admin"),"aaa",http.StatusOK,"read for beamline"},
{"test", "test","read","180",prepareToken("bla"),"",http.StatusBadRequest,"both beamline/beamtime given"},
{"", "","read","180",prepareToken("bla"),"",http.StatusBadRequest,"beamline or beamtime not given"},
{"test", "","bla","180",prepareToken("bla"),"",http.StatusBadRequest,"wrong role"},
{"test", "","read","aaa",prepareToken("bla"),"",http.StatusBadRequest,"wrong duration"},
{"test", "","read","180",prepareToken("bla"),"",http.StatusUnauthorized,"wrong admin token"},
}
func TestIssueToken(t *testing.T) {
for _, test := range IssueTokenTests {
authJWT = utils.NewJWTAuth("secret")
path := "/admin/issue"+"?beamtime="+test.beamtimeId+"&beamline="+test.beamline+"&valid="+test.validDays+"&role="+test.role
w := doGetRequest(path,test.adminToken)
if w.Code == http.StatusOK {
body, _ := ioutil.ReadAll(w.Body)
claims,_ := utils.CheckJWTToken(string(body),"secret")
cclaims,_:= claims.(*utils.CustomClaims)
var extra_claim utils.AccessTokenExtraClaim
utils.MapToStruct(claims.(*utils.CustomClaims).ExtraClaims.(map[string]interface{}), &extra_claim)
assert.Equal(t, cclaims.Subject , test.beamtimeId+test.beamline, test.message)
day,_:=strconv.Atoi(test.validDays)
assert.Equal(t, cclaims.Duration , time.Duration(24*day)*time.Hour, test.message)
assert.Equal(t, extra_claim.Role , test.role, test.message)
} else {
body, _ := ioutil.ReadAll(w.Body)
fmt.Println(string(body))
}
assert.Equal(t, test.status, w.Code, test.message)
}
}
......@@ -184,7 +184,7 @@ func NewHMACAuth(key string) *HMACAuth {
}
func (a *HMACAuth) Name() string {
return "Bearer"
return "HMAC-SHA-256"
}
......@@ -209,6 +209,7 @@ func (h HMACAuth) GenerateToken(val ...interface{}) (string, error) {
return sha, nil
}
// not used
func ProcessHMACAuth(fn http.HandlerFunc, key string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
......@@ -222,7 +223,7 @@ func ProcessHMACAuth(fn http.HandlerFunc, key string) http.HandlerFunc {
value := "beamline"
if authType == "HMAC-SHA-256" {
if !CheckHMACToken(value, token, key) {
http.Error(w, "Internal authorization error - tocken does not match", http.StatusUnauthorized)
http.Error(w, "Internal authorization error - token does not match", http.StatusUnauthorized)
return
}
} else {
......
......@@ -3,3 +3,7 @@ package utils
type FolderTokenTokenExtraClaim struct {
RootFolder string
}
type AccessTokenExtraClaim struct {
Role string
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment