const.go (1637B)
1 package oauth2 2 3 import ( 4 "crypto/sha256" 5 "encoding/base64" 6 "strings" 7 ) 8 9 // ResponseType the type of authorization request 10 type ResponseType string 11 12 // define the type of authorization request 13 const ( 14 Code ResponseType = "code" 15 Token ResponseType = "token" 16 ) 17 18 func (rt ResponseType) String() string { 19 return string(rt) 20 } 21 22 // GrantType authorization model 23 type GrantType string 24 25 // define authorization model 26 const ( 27 AuthorizationCode GrantType = "authorization_code" 28 PasswordCredentials GrantType = "password" 29 ClientCredentials GrantType = "client_credentials" 30 Refreshing GrantType = "refresh_token" 31 Implicit GrantType = "__implicit" 32 ) 33 34 func (gt GrantType) String() string { 35 if gt == AuthorizationCode || 36 gt == PasswordCredentials || 37 gt == ClientCredentials || 38 gt == Refreshing { 39 return string(gt) 40 } 41 return "" 42 } 43 44 // CodeChallengeMethod PCKE method 45 type CodeChallengeMethod string 46 47 const ( 48 // CodeChallengePlain PCKE Method 49 CodeChallengePlain CodeChallengeMethod = "plain" 50 // CodeChallengeS256 PCKE Method 51 CodeChallengeS256 CodeChallengeMethod = "S256" 52 ) 53 54 func (ccm CodeChallengeMethod) String() string { 55 if ccm == CodeChallengePlain || 56 ccm == CodeChallengeS256 { 57 return string(ccm) 58 } 59 return "" 60 } 61 62 // Validate code challenge 63 func (ccm CodeChallengeMethod) Validate(cc, ver string) bool { 64 switch ccm { 65 case CodeChallengePlain: 66 return cc == ver 67 case CodeChallengeS256: 68 s256 := sha256.Sum256([]byte(ver)) 69 // trim padding 70 a := strings.TrimRight(base64.URLEncoding.EncodeToString(s256[:]), "=") 71 b := strings.TrimRight(cc, "=") 72 return a == b 73 default: 74 return false 75 } 76 }