Skip to content
Snippets Groups Projects
mock.go 2.58 KiB
Newer Older
  • Learn to ignore specific revisions
  • Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    package franceconnect
    
    import (
    	"fmt"
    	"net/http"
    	"strings"
    )
    
    // CreateMockOAuth2 creates a mock OAuth2 serve mux for development purposes
    func CreateMock() *http.ServeMux {
    	mux := http.NewServeMux()
    	// Returns authorization code back to the user
    	mux.HandleFunc("/auth", func(w http.ResponseWriter, r *http.Request) {
    		query := r.URL.Query()
    		redir := strings.Replace(query.Get("redirect_uri"), "/callback", "/api/oidc/callback", 1) + "?state=" + query.Get("state") + "&code=mock_code"
    		http.Redirect(w, r, redir, http.StatusFound)
    	})
    
    	// Returns authorization code back to the user for matcher use case
    	mux.HandleFunc("/auth2", func(w http.ResponseWriter, r *http.Request) {
    		query := r.URL.Query()
    		redir := strings.Replace(query.Get("redirect_uri"), "/callback", "/api/matcher/callback", 1) + "?state=" + query.Get("state") + "&code=mock_code"
    		http.Redirect(w, r, redir, http.StatusFound)
    	})
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	// Returns access token back to the user
    	mux.HandleFunc("/token", func(w http.ResponseWriter, r *http.Request) {
    		w.Header().Set("Content-Type", "application/json")
    		w.Write([]byte(`{"access_token":"988d0dc4-682d-4edd-a6dc-cf6dec915270","token_type":"Bearer","expires_in":60,"id_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2ZjcC5pbnRlZzAxLmRldi1mcmFuY2Vjb25uZWN0LmZyIiwic3ViIjoiYjYwNDhlOTViYjEzNGVjNWIxZDFlMWZhNjlmMjg3MTcyZTkxNzIyYjkzNTRkNjM3YTFiY2YyZWJiMGZkMmVmNXYxIiwiYXVkIjoiMjExMjg2NDMzZTM5Y2NlMDFkYjQ0OGQ4MDE4MWJkZmQwMDU1NTRiMTljZDUxYjNmZTc5NDNmNmIzYjg2YWI2ZSIsImV4cCI6MTYyNDYxMDk4MywiaWF0IjoxNjI0NjEwOTIzLCJub25jZSI6IkFfUkFORE9NX05PTkNFIiwiaWRwIjoiRkMiLCJhY3IiOiJlaWRhczMiLCJhbXIiOm51bGx9.zxfAVxZh9Jy-HnFFvszQf-vu_8PBjXu9oi1oD5E-94c"}`))
    	})
    	// Returns userinfo back to the user
    	mux.HandleFunc("/userinfo", func(w http.ResponseWriter, r *http.Request) {
    		w.Header().Set("Content-Type", "application/json")
    		w.Write([]byte(`{"given_name":"Angela Claire Louise","family_name":"DUBOIS","birthdate":"1962-08-24","gender":"female","birthplace":"75107","birthcountry":"99100","preferred_username":"","sub":"b6048e95bb134ec5b1d1e1fa69f287172e91722b9354d637a1bcf2ebb0fd2ef5v1"}`))
    	})
    
    	mux.HandleFunc("/userinfo2", func(w http.ResponseWriter, r *http.Request) {
    		w.Header().Set("Content-Type", "application/json")
    		w.Write([]byte(`{"given_name":"Paul Louis","family_name":"DUPONT","birthdate":"1962-08-24","gender":"male","birthplace":"75107","birthcountry":"99100","preferred_username":"","sub":"dcc2d409424c519ae0599c8585b585711020bd4035b91633e9eabaa9b7542721v1"}`))
    	})
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	// Logout
    	mux.HandleFunc("/logout", func(w http.ResponseWriter, r *http.Request) {
    		fmt.Fprint(w, "Logout OK")
    	})
    
    	return mux
    }