示例#1
0
func RefreshToken(requestUser *user.User) []byte {
	authBackend, err := auth.InitJwtAuthBackend()
	if err != nil {
		panic(err)
	}

	token, err := authBackend.GenerateToken(requestUser)
	if err != nil {
		panic(err)
	}
	response, err := json.Marshal(TokenAuthentication{token})
	if err != nil {
		panic(err)
	}
	return response
}
示例#2
0
func Login(requestUser *user.User) (int, []byte) {
	authBackend, err := auth.InitJwtAuthBackend()
	if err != nil {
		return http.StatusInternalServerError, []byte("")
	}

	if authBackend.Authenticate(requestUser) {
		token, err := authBackend.GenerateToken(requestUser)
		if err != nil {
			return http.StatusInternalServerError, []byte("")
		} else {
			response, _ := json.Marshal(TokenAuthentication{token})
			return http.StatusOK, response
		}
	}
	return http.StatusUnauthorized, []byte("")
}
示例#3
0
func Logout(req *http.Request) error {
	authBackend, err := auth.InitJwtAuthBackend()
	if err != nil {
		return err
	}

	token, err := jwt.ParseFromRequest(
		req,
		func(token *jwt.Token) (interface{}, error) {
			return authBackend.PublicKey, nil
		},
	)

	if err != nil {
		return err
	}

	tokenString := req.Header.Get("Authorization")
	return authBackend.TerminateToken(tokenString, token)
}