security/controller.go
func (c *OAuth2Controller) Token(w http.ResponseWriter, r *http.Request) {
	grantType := r.URL.Query().Get("grant_type")
	code := r.URL.Query().Get("code")
	if strings.Compare(grantType, "authorization_code") != 0 {
		http.Error(w, errors.New("Parameter grant_type is required").Error(), http.StatusBadRequest)
		return
	}
	if strings.Compare(code, "") == 0 {
		http.Error(w, errors.New("Parameter code is required").Error(), http.StatusBadRequest)
		return
	}
	response, err := DecodeOAuth2Code(code, c.cnf.Jwt.Key)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	i, err := strconv.ParseInt(response.Exp, 10, 64)
	exp := time.Unix(i, 0)
	if exp.After(time.Now()) {
		log.Printf("Code is expired")
	} else {
		token := jwt.New(jwt.GetSigningMethod("HS256"))
		claims := token.Claims.(jwt.MapClaims)
		claims["exp"] = time.Now().Add(time.Hour * 1).Unix()
		tokenString, _ := token.SignedString([]byte(c.cnf.Jwt.Key))

		refreshToken := jwt.New(jwt.GetSigningMethod("HS256"))
		refreshClaims := refreshToken.Claims.(jwt.MapClaims)
		refreshClaims["exp"] = 0
		refreshTokenString, _ := refreshToken.SignedString([]byte(c.cnf.Jwt.Key))
		data := map[string]string{
			"access_token":  tokenString,
			"token_type":    "bearer",
			"refresh_token": refreshTokenString,
			"expires_in":    "3600",
		}
		js, _ := json.Marshal(data)
		w.Write(js)
	}
}
func (c *OAuth2Controller) Token(w http.ResponseWriter, r *http.Request) {
	grantType := r.URL.Query().Get("grant_type")
	code := r.URL.Query().Get("code")
	if strings.Compare(grantType, "authorization_code") != 0 {
		http.Error(w, errors.New("Parameter grant_type is required").Error(), http.StatusBadRequest)
		return
	}
	if strings.Compare(code, "") == 0 {
		http.Error(w, errors.New("Parameter code is required").Error(), http.StatusBadRequest)
		return
	}
	response, err := DecodeOAuth2Code(code, c.cnf.Jwt.Key)
	if err != nil {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}
	i, err := strconv.ParseInt(response.Exp, 10, 64)
	exp := time.Unix(i, 0)
	if exp.After(time.Now()) {
		log.Printf("Code is expired")
	} else {
		token := jwt.New(jwt.GetSigningMethod("HS256"))
		claims := token.Claims.(jwt.MapClaims)
		claims["exp"] = time.Now().Add(time.Hour * 1).Unix()
		tokenString, _ := token.SignedString([]byte(c.cnf.Jwt.Key))

		refreshToken := jwt.New(jwt.GetSigningMethod("HS256"))
		refreshClaims := refreshToken.Claims.(jwt.MapClaims)
		refreshClaims["exp"] = 0
		refreshTokenString, _ := refreshToken.SignedString([]byte(c.cnf.Jwt.Key))
		data := map[string]string{
			"access_token":  tokenString,
			"token_type":    "bearer",
			"refresh_token": refreshTokenString,
			"expires_in":    "3600",
		}
		js, _ := json.Marshal(data)
		w.Write(js)
	}
}
//Token returns a new access token