yeungalan пре 5 година
родитељ
комит
c5c8d6f166
3 измењених фајлова са 73 додато и 62 уклоњено
  1. BIN
      __debug_bin
  2. 7 2
      login.html
  3. 66 60
      main.go

+ 7 - 2
login.html

@@ -53,7 +53,7 @@
             <h2 class="ui teal image header">
                 <img src="asset/cuhk.png" class="image" style="width:100%">
             </h2>
-            <form class="ui large form" action="/chkLogin" method="GET">
+            <form class="ui large form" action="/chklogin" method="POST">
                 <div class="ui segment">
                     <div class="field">
                         <h3 class="ui header">Login to {{service-name}}</h3>
@@ -77,7 +77,12 @@
                 </div>
 
                 <div class="ui error message"></div>
-
+                <input type="hidden" name="response_type" value="{{response_type}}">
+                <input type="hidden" name="client_id" value="{{client_id}}">
+                <input type="hidden" name="redirect_uri" value="{{redirect_uri}}">
+                <input type="hidden" name="scope" value="{{scope}}">
+                <input type="hidden" name="state" value="{{state}}">
+                <input type="hidden" name="nonce" value="{{nonce}}">
             </form>
 
             <div class="ui message">

+ 66 - 60
main.go

@@ -25,53 +25,75 @@ type ClientsInformationStruct []struct {
 
 func main() {
 	http.HandleFunc("/login", loginHandler)
+	http.HandleFunc("/chklogin", chkloginHandler)
 	http.Handle("/asset/", http.StripPrefix("/asset/", http.FileServer(http.Dir("./asset"))))
 
 	http.ListenAndServe(":8080", nil)
 }
 
 func loginHandler(w http.ResponseWriter, r *http.Request) {
-	responseType, ok := r.URL.Query()["response_type"]
-	if !ok || len(responseType[0]) < 1 {
-		errHandler(w, r, "Url param response_type was missing")
-		return
-	}
-	if string(responseType[0]) != "code" {
-		errHandler(w, r, "Url param response_type was incompatible")
-		return
-	}
+	_, _, ServiceName, ServiceImage, responseType, clientID, redirectURI, scope, state, nonce := confirmClientInformation(w, r)
 
-	clientID, ok := r.URL.Query()["client_id"]
-	if !ok || len(clientID[0]) < 1 {
-		errHandler(w, r, "Url param client_id was missing")
-		return
-	}
+	//serve file
+	//push assembled data to page
+	parsedPage, err := templateLoad("login.html", map[string]interface{}{
+		"service-name":  string(ServiceName),
+		"service-image": string(ServiceImage),
+		"response_type": string(responseType),
+		"client_id":     string(clientID),
+		"redirect_uri":  string(redirectURI),
+		"scope":         string(scope),
+		"state":         string(state),
+		"nonce":         string(nonce),
+	})
 
-	redirectURI, ok := r.URL.Query()["redirect_uri"]
-	if !ok || len(redirectURI[0]) < 1 {
-		errHandler(w, r, "Url param redirect_uri was missing")
-		return
+	if err != nil {
+		log.Println("Error. Unable to load login.html")
 	}
-	redirectURIParsed, err := url.Parse(redirectURI[0])
-	host, _, _ := net.SplitHostPort(redirectURIParsed.Host)
+	w.Write([]byte(parsedPage))
+}
 
-	scope, ok := r.URL.Query()["scope"]
-	if !ok || len(scope[0]) < 1 {
-		errHandler(w, r, "Url param scope was missing")
+func chkloginHandler(w http.ResponseWriter, r *http.Request) {
+	// Call ParseForm() to parse the raw query and update r.PostForm and r.Form.
+	if err := r.ParseForm(); err != nil {
+		fmt.Fprintf(w, "ParseForm() err: %v", err)
 		return
 	}
+	fmt.Fprintf(w, "Post from website! r.PostFrom = %v\n", r.PostForm)
+	email := r.FormValue("email")
+	password := r.FormValue("password")
+	fmt.Println(email, password)
+}
 
-	state, ok := r.URL.Query()["state"]
-	if !ok || len(state[0]) < 1 {
-		errHandler(w, r, "Url param state was missing")
-		return
+func errHandler(w http.ResponseWriter, r *http.Request, errorMsg string) {
+	//push assembled data to page
+	parsedPage, err := templateLoad("error.html", map[string]interface{}{
+		"error": string(errorMsg),
+	})
+
+	if err != nil {
+		log.Println("Error. Unable to show error. Additionally, the error page also had error.")
 	}
+	w.Write([]byte(parsedPage))
+	//sendTextResponse(w, parsedPage)
+}
+
+func confirmClientInformation(w http.ResponseWriter, r *http.Request) (string, string, string, string, string, string, string, string, string, string) {
+	responseType := getGET(w, r, "response_type")
+	if string(responseType) != "code" {
+		errHandler(w, r, "Url param response_type was incompatible")
 
-	nonce, ok := r.URL.Query()["nonce"]
-	if !ok || len(nonce[0]) < 1 {
-		errHandler(w, r, "Url param nonce was missing")
-		return
 	}
+	//
+	clientID := getGET(w, r, "client_id")
+	//
+	redirectURI := getGET(w, r, "redirect_uri")
+	redirectURIParsed, err := url.Parse(redirectURI)
+	host, _, _ := net.SplitHostPort(redirectURIParsed.Host)
+	//
+	scope := getGET(w, r, "scope")
+	state := getGET(w, r, "state")
+	nonce := getGET(w, r, "nonce")
 
 	//let say the GET request was good, then let us find does client-id and domain match our record
 	data, err := ioutil.ReadFile("./client-id.json")
@@ -85,15 +107,15 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
 	}
 	//process DB
 	DBClientID := ""
-	//DBClientSecret := ""
+	DBClientSecret := ""
 	DBDomain := ""
 	DBServiceName := ""
 	DBServiceImage := ""
 	var DBScope []string
 	for _, ClientInformation := range ClientsInformation {
-		if ClientInformation.ClientID == clientID[0] {
+		if ClientInformation.ClientID == clientID {
 			DBClientID = ClientInformation.ClientID
-			//DBClientSecret = ClientInformation.ClientSecret
+			DBClientSecret = ClientInformation.ClientSecret
 			DBDomain = ClientInformation.Domain
 			DBServiceName = ClientInformation.ServiceName
 			DBServiceImage = ClientInformation.ServiceImage
@@ -102,46 +124,30 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
 	}
 	if DBClientID == "" {
 		errHandler(w, r, "client_id does not exist in our database.")
-		return
+
 	}
 	//check the record does it match
 	if DBDomain != host {
 		errHandler(w, r, "client_id and redirect_uri not match the system record.")
-		return
+
 	}
 	//check if scope is available for that client
-	scopeArr := strings.Split(scope[0], " ")
+	scopeArr := strings.Split(scope, " ")
 	for _, scopeItem := range scopeArr {
 		if !contains(DBScope, scopeItem) {
 			errHandler(w, r, "scope not match our system record.")
-			return
-		}
-	}
-
-	//serve file
-	//push assembled data to page
-	parsedPage, err := templateLoad("login.html", map[string]interface{}{
-		"service-name":  string(DBServiceName),
-		"service-image": string(DBServiceImage),
-	})
 
-	if err != nil {
-		log.Println("Error. Unable to show error. Additionally, the error page also had error.")
+		}
 	}
-	w.Write([]byte(parsedPage))
+	return DBClientSecret, DBDomain, DBServiceName, DBServiceImage, responseType, clientID, redirectURI, scope, state, nonce
 }
 
-func errHandler(w http.ResponseWriter, r *http.Request, errorMsg string) {
-	//push assembled data to page
-	parsedPage, err := templateLoad("error.html", map[string]interface{}{
-		"error": string(errorMsg),
-	})
-
-	if err != nil {
-		log.Println("Error. Unable to show error. Additionally, the error page also had error.")
+func getGET(w http.ResponseWriter, r *http.Request, name string) string {
+	response, ok := r.URL.Query()[name]
+	if !ok || len(response[0]) < 1 {
+		errHandler(w, r, "Url param "+name+" was missing")
 	}
-	w.Write([]byte(parsedPage))
-	//sendTextResponse(w, parsedPage)
+	return response[0]
 }
 
 func templateLoad(filename string, replacement map[string]interface{}) (string, error) {