generated from cjlapao/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
introspection.go
52 lines (43 loc) · 1.4 KB
/
introspection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package controllers
import (
"encoding/json"
"fmt"
"net/http"
"github.com/cjlapao/common-go-identity/jwt"
"github.com/cjlapao/common-go-identity/models"
"github.com/cjlapao/common-go-restapi/controllers"
)
// Introspection Validates a token in the context returning an openid oauth introspect response
func (c *AuthorizationControllers) Introspection() controllers.Controller {
return func(w http.ResponseWriter, r *http.Request) {
ctx := NewBaseContext(r)
token := r.FormValue("token")
if token == "" {
w.WriteHeader(http.StatusUnauthorized)
ErrEmptyToken.Log()
json.NewEncoder(w).Encode(ErrEmptyToken)
return
}
userToken, err := jwt.ValidateUserToken(token, ctx.AuthorizationContext)
if err != nil {
response := models.OAuthIntrospectResponse{
Active: false,
}
ErrInvalidToken.Log()
c.Logger.Error("Token for user %v is not valid, %v", userToken.DisplayName, err.Error())
json.NewEncoder(w).Encode(response)
return
}
response := models.OAuthIntrospectResponse{
Active: true,
ID: userToken.ID,
TokenType: userToken.Scope,
Subject: userToken.User,
ExpiresAt: fmt.Sprintf("%v", userToken.ExpiresAt.Unix()),
IssuedAt: fmt.Sprintf("%v", userToken.IssuedAt.Unix()),
Issuer: userToken.Issuer,
}
ctx.Logger.Success("Token for user %v was validated successfully", userToken.DisplayName)
json.NewEncoder(w).Encode(response)
}
}