Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxying Meet API and make sure gitlab is using correct personal token for current user #892

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ type token struct {
Token string `json:"token"`
}

// Proxy function that forwards the request to the target URL
func (repman *ReplicationManager) proxyToURL(target string) http.Handler {
url, err := url.Parse(target)
if err != nil {
log.Fatalf("Failed to parse target URL: %v", err)
}

proxy := httputil.NewSingleHostReverseProxy(url)

return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Modify the request as needed before forwarding
r.Host = url.Host
proxy.ServeHTTP(w, r)
})
}

func (repman *ReplicationManager) SharedirHandler(folder string) http.Handler {
sub, err := fs.Sub(share.EmbededDbModuleFS, folder)
if err != nil {
Expand Down Expand Up @@ -176,6 +192,8 @@ func (repman *ReplicationManager) apiserver() {
router.PathPrefix("/graphite/").Handler(http.StripPrefix("/graphite/", graphiteProxy))
}

router.PathPrefix("/meet/").Handler(http.StripPrefix("/meet/", repman.proxyToURL("https://meet.signal18.io/api/v4")))

if repman.Conf.Test {
router.HandleFunc("/", repman.handlerApp)
router.PathPrefix("/images/").Handler(http.FileServer(http.Dir(repman.Conf.HttpRoot)))
Expand Down
2 changes: 2 additions & 0 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func (repman *ReplicationManager) httpserver() {
router.PathPrefix("/graphite/").Handler(http.StripPrefix("/graphite/", graphiteProxy))
}

router.PathPrefix("/meet/").Handler(http.StripPrefix("/meet/", repman.proxyToURL("https://meet.signal18.io/api/v4")))

//router.HandleFunc("/", repman.handlerApp)
// page to view which does not need authorization
if repman.Conf.Test {
Expand Down
43 changes: 42 additions & 1 deletion utils/githelper/githelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,16 @@ type AccessToken struct {

func GetGitLabTokenOAuth(acces_token string, log_git bool) (string, int) {

req, err := http.NewRequest("GET", "https://gitlab.signal18.io/api/v4/personal_access_tokens?revoked=false", nil)
uid, _, err := GetGitLabUserId(acces_token)
if err != nil {
return "", -1
}

if uid == 0 {
return "", -1
}

req, err := http.NewRequest("GET", fmt.Sprintf("https://gitlab.signal18.io/api/v4/personal_access_tokens?revoked=false&user_id=%d", uid), nil)
if err != nil {
log.Println("Gitlab API Error: ", err)
}
Expand Down Expand Up @@ -259,3 +268,35 @@ func GetGitLabTokenBasicAuth(user string, password string, log_git bool) string
return accessToken.AccessToken

}

type UserId struct {
ID int `json:"id"`
}

func GetGitLabUserId(acces_token string) (int, []byte, error) {
var body = make([]byte, 0)

req, err := http.NewRequest("GET", "https://gitlab.signal18.io/api/v4/user", nil)
if err != nil {
return 0, body, fmt.Errorf("Gitlab User API Error: ", err)
}
req.Header.Set("Authorization", "Bearer "+acces_token)

resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, body, fmt.Errorf("Gitlab User API Error: ", err)
}
defer resp.Body.Close()

body, _ = io.ReadAll(resp.Body)

var userId UserId

err = json.Unmarshal(body, &userId)
if err != nil {
return 0, body, fmt.Errorf("Gitlab User API Unmarshall Error: ", err)
}

return userId.ID, body, nil

}