Select Git revision
stack_stack.h
main.go 4.28 KiB
package main
jwtverifier "github.com/okta/okta-jwt-verifier-golang"
import (
"net/http"
"github.com/gin-gonic/gin"
"fmt"
"os"
"strings"
)
type student struct {
ID string `json:"id"`
Lastname string `json:"lastname"`
Name string `json:"name"`
Filiere string `json:"software"`
}
type teacher struct {
ID string `json:"id"`
Lastname string `json:"lastname"`
Name string `json:"name"`
Filiere string `json:"classname"`
}
var teachers = []teacher{
{ID: "1", Lastname: "Ouaffi", Name: "Kaled", Filiere: "ISC"},
{ID: "2", Lastname: "Ou", Name: "Est", Filiere: "LeTuto"},
}
var students = []student{
{ID: "1", Lastname: "Stein", Name: "Vin", Class: "ISC"},
{ID: "2", Lastname: "Vrinom", Name: "Jean", Class: "Rien"},
}
func getTeachers(c *gin.Context) {
c.IndentedJSON(http.StatusOK, teachers)
}
func getStudents(c *gin.Context) {
c.IndentedJSON(http.StatusOK, students)
}
func postTeachers(c *gin.Context) {
var newTeacher teacher
if err := c.BindJSON(&newTeacher); err != nil {
return
}
teachers = append(teachers, newTeacher)
c.IndentedJSON(http.StatusCreated, newTeacher)
}
func postStudents(c *gin.Context) {
var newStudent student
if err := c.BindJSON(&newStudent); err != nil {
return
}
students = append(students, newStudent)
c.IndentedJSON(http.StatusCreated, newStudent)
}
func getTeacherByID(c *gin.Context) {
id := c.Param("id")
for _, a := range teachers {
if a.ID == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "teacher not found"})
}
func getStudentByID(c *gin.Context) {
id := c.Param("id")
for _, a := range students {
if a.ID == id {
c.IndentedJSON(http.StatusOK, a)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "student not found"})
}
func deleteTeacherByID(c *gin.Context) {
id := c.Param("id")
for i, a := range teachers {
if a.ID == id {
c.IndentedJSON(http.StatusAccepted, a)
teachers = append(teachers[:i], teachers[i+1:]...)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "teacher not found"})
}
func deleteStudentByID(c *gin.Context) {
id := c.Param("id")
for i, a := range students {
if a.ID == id {
c.IndentedJSON(http.StatusAccepted, a)
students = append(students[:i], students[i+1:]...)
return
}
}
c.IndentedJSON(http.StatusNotFound, gin.H{"message": "student not found"})
}
func past_main() {
router := gin.Default()
router.GET("/teachers", getTeachers)
router.GET("/students", getStudents)
router.GET("/teachers/:id", getTeacherByID)
router.GET("/students/:id", getStudentByID)
router.POST("/teachers", postTeachers)
router.POST("/students", postStudents)
router.DELETE("/teachers/:id", deleteTeacherByID)
router.DELETE("/students/:id", deleteStudentByID)
router.Run("localhost:8080")
}
var toValidate = map[string]string{
"aud": "api://default",
"cid": os.Getenv("0oa3le9vcoY0Hf2T75d7"),
}
func verify(c *gin.Context) bool {
status := true
token := c.Request.Header.Get("Authorization")
if strings.HasPrefix(token, "Bearer ") {
token = strings.TrimPrefix(token, "Bearer ")
verifierSetup := jwtverifier.JwtVerifier{
Issuer: "https://" + os.Getenv("dev-75048500.okta.com") + "/oauth2/default",
ClaimsToValidate: toValidate,
}
verifier := verifierSetup.New()
_, err := verifier.VerifyAccessToken(token)
if err != nil {
c.String(http.StatusForbidden, err.Error())
print(err.Error())
status = false
}
} else {
c.String(http.StatusUnauthorized, "Unauthorized")
status = false
}
return status
}
func AddListItem(c *gin.Context) {
if verify(c) {
item := c.PostForm("item")
todos = append(todos, item)
c.String(http.StatusCreated, c.FullPath()+"/"+strconv.Itoa(len(todos)-1))
}
}
func main() {
r := gin.Default()
accounts := make(map[string]string)
authorized := r.Group("/", gin.BasicAuth(gin.Accounts{
"user1": "first",
"user2": "second",
}))
authorized.GET("/secret", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"secret": "The secret.",})
})
os.Setenv("FOO", "1")
fmt.Println("FOO:", os.Getenv("FOO"))
fmt.Println("BAR:", os.Getenv("BAR"))
fmt.Println()
for _, e := range os.Environ() {
pair := strings.SplitN(e, "=", 2)
fmt.Println(pair[0])
}
r.Run("0.0.0.0:8080")
}