This repository has been archived by the owner on Jun 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Julio Galvan
committed
Jun 6, 2019
1 parent
2ca0282
commit f9ab972
Showing
1 changed file
with
48 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,84 +1,64 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"encoding/json" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
type AuthRequest struct { | ||
Auth struct { | ||
Mxid string `json:"mxid"` | ||
Localpart string `json:"localpart"` | ||
Domain string `json:"domain"` | ||
Password string `json:"password"` | ||
} `json:"auth"` | ||
Auth struct { | ||
Mxid string `json:"mxid"` | ||
Localpart string `json:"localpart"` | ||
Domain string `json:"domain"` | ||
Password string `json:"password"` | ||
} `json:"auth"` | ||
} | ||
|
||
type AuthResponse struct { | ||
Auth AuthResponseBody `json:"auth"` | ||
Auth AuthResponseBody `json:"auth"` | ||
} | ||
|
||
type AuthResponseBody struct { | ||
Success bool `json:"success"` | ||
Id *AuthResponseBodyId `json:"id,omitempty"` | ||
Profile *AuthResponseBodyProfile `json:"profile,omitempty"` | ||
Success bool `json:"success"` | ||
ID *LookUpID `json:"id,omitempty"` | ||
Profile *ProfileBody `json:"profile,omitempty"` | ||
} | ||
|
||
type AuthResponseBodyId struct { | ||
Type string `json:"type,omitempty"` | ||
Value string `json:"value,omitempty"` | ||
} | ||
func Authentication(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != "POST" { | ||
return | ||
} | ||
var req AuthRequest | ||
if json.NewDecoder(r.Body).Decode(&req) != nil { | ||
fmt.Println("Invalid json request") | ||
return | ||
} | ||
res := AuthResponse{ | ||
Auth: AuthResponseBody{ | ||
Success: false, | ||
}, | ||
} | ||
_, err := loginKeycloak(req.Auth.Localpart, req.Auth.Password) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
profile, err := buildProfile(req.Auth.Localpart) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
res = AuthResponse{ | ||
Auth: AuthResponseBody{ | ||
Success: true, | ||
ID: &LookUpID{ | ||
Type: "localpart", | ||
Value: req.Auth.Localpart, | ||
}, | ||
Profile: profile, | ||
}, | ||
} | ||
} | ||
} | ||
prepareResponse(w, res) | ||
|
||
type AuthResponseBodyProfile struct { | ||
DisplayName string `json:"display_name,omitempty"` | ||
ThreePIDS []Profile3PIDS `json:"three_pids,omitempty"` | ||
} | ||
|
||
func Authentication(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == "POST" { | ||
decoder := json.NewDecoder(r.Body) | ||
var req AuthRequest | ||
err := decoder.Decode(&req) | ||
if err != nil { | ||
fmt.Println("Invalid json request") | ||
return | ||
} | ||
res := AuthResponse { | ||
Auth: AuthResponseBody { | ||
Success: false, | ||
}, | ||
} | ||
_, err = loginKeycloak(req.Auth.Localpart, req.Auth.Password) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
token, err := getKeycloakToken() | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
user, err := getUserArray(token, req.Auth.Localpart) | ||
if err != nil { | ||
fmt.Println(err) | ||
} else { | ||
res = AuthResponse { | ||
Auth: AuthResponseBody { | ||
Success: true, | ||
Id: &AuthResponseBodyId { | ||
Type: "localpart", | ||
Value: req.Auth.Localpart, | ||
}, | ||
Profile: &AuthResponseBodyProfile { | ||
DisplayName: getDisplayName(user), | ||
ThreePIDS: getProfile3PIDS(user), | ||
}, | ||
}, | ||
} | ||
} | ||
} | ||
} | ||
w.Header().Set("Access-Control-Allow-Origin", "*") | ||
w.Header().Set("Content-Type", "application/json") | ||
json.NewEncoder(w).Encode(res) | ||
} | ||
} |