Auth: Improve oidc test provider #782

This commit is contained in:
Timo Volkmann 2021-09-20 19:18:52 +02:00
parent a7400d6477
commit f278fb69c1
2 changed files with 26 additions and 44 deletions

View file

@ -124,7 +124,8 @@ func (s *AuthStorage) Health(ctx context.Context) error {
return nil
}
func (s *AuthStorage) CreateAuthRequest(_ context.Context, authReq *oidc.AuthRequest, _ string) (op.AuthRequest, error) {
func (s *AuthStorage) CreateAuthRequest(_ context.Context, authReq *oidc.AuthRequest, userId string) (op.AuthRequest, error) {
fmt.Println("Userid: ", userId)
fmt.Println("CreateAuthRequest ID: ", authReq.ID)
fmt.Println("CreateAuthRequest CodeChallenge: ", authReq.CodeChallenge)
fmt.Println("CreateAuthRequest CodeChallengeMethod: ", authReq.CodeChallengeMethod)
@ -133,7 +134,10 @@ func (s *AuthStorage) CreateAuthRequest(_ context.Context, authReq *oidc.AuthReq
fmt.Println("CreateAuthRequest ResponseType: ", authReq.ResponseType)
fmt.Println("CreateAuthRequest Nonce: ", authReq.Nonce)
fmt.Println("CreateAuthRequest Scopes: ", authReq.Scopes)
a = &AuthRequest{ID: "loginId", ClientID: authReq.ClientID, ResponseType: authReq.ResponseType, Nonce: authReq.Nonce, RedirectURI: authReq.RedirectURI}
fmt.Println("CreateAuthRequest Display: ", authReq.Display)
fmt.Println("CreateAuthRequest LoginHint: ", authReq.LoginHint)
fmt.Println("CreateAuthRequest IDTokenHint: ", authReq.IDTokenHint)
a = &AuthRequest{ID: "authReqUserAgentId", ClientID: authReq.ClientID, ResponseType: authReq.ResponseType, Nonce: authReq.Nonce, RedirectURI: authReq.RedirectURI}
if authReq.CodeChallenge != "" {
a.CodeChallenge = &oidc.CodeChallenge{
Challenge: authReq.CodeChallenge,
@ -152,7 +156,7 @@ func (s *AuthStorage) AuthRequestByCode(_ context.Context, code string) (op.Auth
}
func (s *AuthStorage) SaveAuthCode(_ context.Context, id, code string) error {
if a.ID != id {
return errors.New("not found")
return errors.New("SaveAuthCode: not found")
}
c = code
return nil
@ -162,8 +166,9 @@ func (s *AuthStorage) DeleteAuthRequest(context.Context, string) error {
return nil
}
func (s *AuthStorage) AuthRequestByID(_ context.Context, id string) (op.AuthRequest, error) {
if id != "loginId" || t {
return nil, errors.New("not found")
fmt.Println("AuthRequestByID: ", id)
if id != "authReqUserAgentId:usertoken" || t {
return nil, errors.New("AuthRequestByID: not found")
}
return a, nil
}
@ -227,7 +232,7 @@ func (s *AuthStorage) GetKeyByIDAndUserID(_ context.Context, _, _ string) (*jose
func (s *AuthStorage) GetClientByClientID(_ context.Context, id string) (op.Client, error) {
if id == "none" {
return nil, errors.New("not found")
return nil, errors.New("GetClientByClientID: not found")
}
var appType op.ApplicationType
var authMethod oidc.AuthMethod
@ -319,8 +324,8 @@ func (c *ConfClient) PostLogoutRedirectURIs() []string {
}
func (c *ConfClient) LoginURL(id string) string {
return "authorize/callback?id=" + id
//return "login?id=" + id
//return "authorize/callback?id=" + id
return "login?id=" + id
}
func (c *ConfClient) ApplicationType() op.ApplicationType {

View file

@ -2,6 +2,7 @@ package main
import (
"context"
"crypto/rand"
"crypto/sha256"
"log"
"net/http"
@ -14,24 +15,24 @@ import (
func main() {
ctx := context.Background()
b := make([]byte, 32)
rand.Read(b)
port := "9998"
config := &op.Config{
Issuer: "http://host.docker.internal:9998",
CryptoKey: sha256.Sum256([]byte("test0123test0123test0123test0123")),
Issuer: "http://host.docker.internal:9998",
CryptoKey: sha256.Sum256(b),
CodeMethodS256: true,
}
storage := mock.NewAuthStorage()
//opts := []op.Option{
//}
//
handler, err := op.NewOpenIDProvider(ctx, config, storage)
if err != nil {
log.Fatal(err)
}
router := handler.HttpHandler().(*mux.Router)
router.Methods("GET").Path("/login").HandlerFunc(HandleLogin)
//router.Methods("POST").Path("/login").HandlerFunc(HandleCallback)
server := &http.Server{
Addr: ":" + port,
Handler: router,
@ -44,34 +45,10 @@ func main() {
}
func HandleLogin(w http.ResponseWriter, r *http.Request) {
//tpl := `
//<!DOCTYPE html>
//<html>
// <head>
// <meta charset="UTF-8">
// <title>Login</title>
// </head>
// <body>
// <form method="POST" action="/login">
// <input name="client"/>
// <button type="submit">Login</button>
// </form>
// </body>
//</html>`
//t, err := template.New("login").Parse(tpl)
//if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
// return
//}
//err = t.Execute(w, nil)
//if err != nil {
// http.Error(w, err.Error(), http.StatusInternalServerError)
//}
http.Redirect(w, r, "/authorize/callback?id=loginId", http.StatusFound)
}
func HandleCallback(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
client := r.FormValue("client")
http.Redirect(w, r, "/authorize/callback?id="+client, http.StatusFound)
requestId := r.Form.Get("id")
// simulate user login and retrieve a token that indicates a successfully logged-in user
usertoken := requestId + ":usertoken"
http.Redirect(w, r, "/authorize/callback?id="+usertoken, http.StatusFound)
}