Skip to content
Snippets Groups Projects
Select Git revision
  • ee59aa75d848e95a915776a6f5ac27ed7a346cf1
  • main default protected
2 results

oauth.go

Blame
  • user avatar
    Yege1893 authored
    ee59aa75
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    oauth.go 989 B
    package handler
    
    import (
    	"context"
    	"net/http"
    
    	log "github.com/sirupsen/logrus"
    	"gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/highlanderticketing/config"
    	"gitlab.reutlingen-university.de/ege/highlander-ticketing-go-ss2023/src/highlanderticketing/service"
    
    	"golang.org/x/oauth2"
    )
    
    func HandleLogin(w http.ResponseWriter, r *http.Request) {
    	oauthConfig := config.GetOAuthConfig()
    	url := oauthConfig.AuthCodeURL("state", oauth2.AccessTypeOffline)
    	http.Redirect(w, r, url, http.StatusTemporaryRedirect)
    }
    
    func HandleCallback(w http.ResponseWriter, r *http.Request) {
    	oauthConfig := config.GetOAuthConfig()
    	code := r.URL.Query().Get("code")
    	token, err := oauthConfig.Exchange(context.Background(), code)
    	if err != nil {
    		log.Println("Fehler beim Austausch des Autorisierungscodes:", err)
    		http.Error(w, "Fehler beim Authentifizieren", http.StatusInternalServerError)
    		return
    	}
    	service.Register(token.AccessToken)
    	sendJson(w, token.AccessToken)
    }