/
oauth2.go
70 lines (56 loc) · 1.75 KB
/
oauth2.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package pitchfork
/* TODO: verify against https://tools.ietf.org/html/rfc7523 */
import (
"errors"
)
type OAuth_Auth struct {
ClientID string `label:"Client ID" pfset:"nobody" pfget:"none"`
Scope string `label:"Scope" pfset:"nobody" pfget:"none"`
RType string `label:"Request Type" pfset:"nobody" pfget:"none"`
Redirect string `label:"Redirect URL" pfset:"nobody" pfget:"none"`
Auth string `label:"Authorize" pftype:"submit"`
Deny string `label:"Deny" pftype:"submit" htmlclass:"deny"`
}
type OAuth2Claims struct {
JWTClaims
ClientID string `json:"oa_client_id"`
Scope string `json:"oa_scope"`
RType string `json:"oa_rtype,omitempty"`
Redirect string `json:"oa_redirect,omitempty"`
}
func OAuth2_AuthToken_New(ctx PfCtx, o OAuth_Auth) (tok string, err error) {
if !ctx.IsLoggedIn() {
tok = ""
err = errors.New("Not authenticated")
return
}
/* Auth Tokens expire after an hour */
claims := &OAuth2Claims{}
claims.ClientID = o.ClientID
claims.Scope = o.Scope
claims.RType = o.RType
claims.Redirect = o.Redirect
username := ctx.TheUser().GetUserName()
token := Token_New("oauth_auth", username, 1, claims)
tok, err = token.Sign()
return
}
func OAuth2_AuthToken_Check(tok string) (claims *OAuth2Claims, err error) {
_, err = Token_Parse(tok, "oauth_auth", claims)
return
}
func OAuth2_AccessToken_New(ctx PfCtx, client_id string, scope string) (tok string, err error) {
if !ctx.IsLoggedIn() {
tok = ""
err = errors.New("Not authenticated")
return
}
claims := &OAuth2Claims{}
claims.ClientID = client_id
claims.Scope = scope
username := ctx.TheUser().GetUserName()
/* Access Tokens - 24 hour validity */
token := Token_New("oauth_access", username, TOKEN_EXPIRATIONMINUTES, claims)
tok, err = token.Sign()
return
}