-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RBAC done using google cloud authentication
- Loading branch information
1 parent
56d52fd
commit 33a203f
Showing
12 changed files
with
270 additions
and
157 deletions.
There are no files selected for viewing
Binary file not shown.
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"html/template" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/gorilla/sessions" | ||
"github.com/markbates/goth" | ||
"github.com/markbates/goth/gothic" | ||
"github.com/markbates/goth/providers/google" | ||
) | ||
|
||
func AuthHandler(router *mux.Router, config *Config) { | ||
maxAge := 86400 * 30 // 30 days | ||
isProd := true // Set to true when serving over https | ||
|
||
store := sessions.NewCookieStore([]byte(config.SessionSecret)) | ||
store.MaxAge(maxAge) | ||
store.Options.Path = "/" | ||
store.Options.HttpOnly = true // HttpOnly should always be enabled | ||
store.Options.Secure = isProd | ||
|
||
gothic.Store = store | ||
goth.UseProviders(google.New(config.GoogleClientID, config.GoogleClientSecret, "https://localhost:8080/auth/google/callback", "email", "profile")) | ||
|
||
router.HandleFunc("/auth/{provider}/callback", func(res http.ResponseWriter, req *http.Request) { | ||
user, err := gothic.CompleteUserAuth(res, req) | ||
if err != nil { | ||
fmt.Fprintln(res, err) | ||
return | ||
} | ||
|
||
userObj, userRole, err := getUserRole(user.Email) | ||
if err != nil { | ||
fmt.Fprintln(res, err) | ||
return | ||
} | ||
log.Println("User role:", userRole) | ||
|
||
// Only store the user object into the session if userRole is not an empty string | ||
if userRole != "" { | ||
// Create a User object with the user role | ||
currentUser := User{ | ||
GoogleID: user.UserID, | ||
Name: user.Name, | ||
Email: user.Email, | ||
ContactNumber: userObj.ContactNumber, // Use contact number from the retrieved user object | ||
Role: userObj.Role, | ||
CreatedAt: userObj.CreatedAt, | ||
UpdatedAt: userObj.UpdatedAt, | ||
} | ||
|
||
// Serialize the user object to JSON | ||
userData, err := json.Marshal(currentUser) | ||
if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Get the session and store the user data | ||
session, err := store.Get(req, "auth-session") | ||
if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
session.Values["user"] = userData | ||
err = session.Save(req, res) | ||
if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
// Redirect based on user role | ||
if userRole == "Admin" { | ||
AdminHandler(router) | ||
http.Redirect(res, req, "/admin", http.StatusFound) | ||
} else if userRole == "Instructor" { | ||
InstructorHandler(router) | ||
http.Redirect(res, req, "/instructor", http.StatusFound) | ||
} else if userRole == "Student" { | ||
StudentHandler(router) | ||
http.Redirect(res, req, "/student", http.StatusFound) | ||
} else if userRole == "Parent" { | ||
ParentHandler(router) | ||
http.Redirect(res, req, "/parent", http.StatusFound) | ||
} | ||
} else { | ||
http.Redirect(res, req, "/unregistered", http.StatusFound) | ||
} | ||
}).Methods("GET") | ||
|
||
router.HandleFunc("/auth/{provider}", func(res http.ResponseWriter, req *http.Request) { | ||
gothic.BeginAuthHandler(res, req) | ||
}).Methods("GET") | ||
|
||
router.HandleFunc("/login", func(res http.ResponseWriter, req *http.Request) { | ||
t, err := template.ParseFiles("templates/login.html") | ||
if err != nil { | ||
http.Error(res, err.Error(), http.StatusInternalServerError) | ||
return | ||
} | ||
t.Execute(res, false) | ||
}).Methods("GET") | ||
} |
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
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
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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
//"github.com/joho/godotenv" | ||
|
||
firebase "firebase.google.com/go" | ||
"google.golang.org/api/option" | ||
) | ||
|
||
// Use godot package to load/read the .env file and | ||
// return the value of the key (for local env) | ||
// func goDotEnvVariable(key string) string { | ||
|
||
// // load .env file | ||
// err := godotenv.Load(".env") | ||
|
||
// if err != nil { | ||
// log.Fatalf("Error loading .env file") | ||
// } | ||
|
||
// return os.Getenv(key) | ||
// } | ||
|
||
// InitializeFirebase initializes the Firebase app and sets the global firebaseClient variable | ||
func initializeFirebase() error { | ||
ctx := context.Background() | ||
|
||
databaseURL, found := os.LookupEnv("DATABASE_URL") | ||
if !found { | ||
log.Fatalf("DATABASE_URL is not set in the environment variables") | ||
} | ||
|
||
// databaseURL := goDotEnvVariable("DATABASE_URL") | ||
// if databaseURL == "" { | ||
// return fmt.Errorf("DATABASE_URL is not set in the environment variables") | ||
// } | ||
|
||
conf := &firebase.Config{DatabaseURL: databaseURL} | ||
|
||
opt := option.WithCredentialsFile("edusync-7bd5e-firebase-adminsdk-x49uh-af084a6314.json") | ||
//opt := option.WithCredentialsFile("edusync-test-firebase-adminsdk-hk5kl-9af0162b09.json") | ||
|
||
app, err := firebase.NewApp(ctx, conf, opt) | ||
if err != nil { | ||
return fmt.Errorf("error initializing firebase app: %v", err) | ||
} | ||
|
||
var firebaseApp = app | ||
|
||
err = initDB(firebaseApp) | ||
if err != nil { | ||
return fmt.Errorf("error initializing database: %v", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.