diff --git a/authentication.go b/authentication.go deleted file mode 100644 index 80fd81d..0000000 --- a/authentication.go +++ /dev/null @@ -1,137 +0,0 @@ -// -// TheFederation Github Integration Server -// Copyright (C) 2018 Lukas Matt -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . -// -package main - -import ( - "fmt" - "net/http" - "golang.org/x/oauth2" - "github.com/google/go-github/github" - "github.com/jinzhu/gorm" - _ "github.com/jinzhu/gorm/dialects/sqlite" - "context" - "strings" -) - -func authentication(w http.ResponseWriter, r *http.Request) { - ctx := context.Background() - accessToken := r.URL.Query().Get("access_token") - repo := r.URL.Query().Get("repo") - project := r.URL.Query().Get("project") - - if accessToken != "" && repo != "" && project != "" { - db, err := gorm.Open(databaseDriver, databaseDSN) - if err != nil { - logger.Println(err) - fmt.Fprintf(w, "Database Failure :(") - return - } - defer db.Close() - - ctx := context.Background() - ts := oauth2.StaticTokenSource( - &oauth2.Token{AccessToken: accessToken}, - ) - tc := oauth2.NewClient(ctx, ts) - client := github.NewClient(tc) - - repoSlice := strings.Split(repo, "/") - if len(repoSlice) < 2 { - logger.Println("invalid repository string") - fmt.Fprintf(w, "Invalid Repository String :(") - return - } - - secret := Secret(16) - repo := Repo{ - Project: project, - Slug: repo, - Token: accessToken, - Secret: secret, - } - - if !devMode { - err = db.Create(&repo).Error - if err != nil { - logger.Println(err) - fmt.Fprintf(w, "Database Insert Failure :(\n%s", - "(the project probably already exists)") - return - } - } - - name := "web" - hook := github.Hook{ - Name: &name, Events: []string{"pull_request"}, - Config: map[string]interface{}{ - "content_type": "json", - "url": serverDomain + "/hook", - "secret": secret, - }, - } - - if !devMode { - _, _, err = client.Repositories.CreateHook(ctx, repoSlice[0], repoSlice[1], &hook) - if err != nil { - logger.Println(err) - fmt.Fprintf(w, "Create Hook Failure :(") - return - } - } - - fmt.Fprintf(w, ` - - -

- Success :) You can undo it by simply deleting the - webhook. -

- - `, repo.Slug) - return - } - - code := r.URL.Query().Get("code") - if code != "" { - tok, err := conf.Exchange(ctx, code) - if !devMode && err != nil { - fmt.Println(err) - fmt.Fprintf(w, "Token Failure :(") - } else { - var token string = "1234" - if !devMode { - token = tok.AccessToken - } - fmt.Fprintf(w, ` - - -

You are authenticated :) Please add your repository:

-
- - - - -
- - `, token) - } - } else { - url := conf.AuthCodeURL("state", oauth2.AccessTypeOffline) - http.Redirect(w, r, url, http.StatusMovedPermanently) - } -}