Skip to content

Commit 9aa4156

Browse files
authored
Merge pull request #3 from StandUpCode/develop
Feat: Verify ID Token
2 parents af946a9 + 8ae5a48 commit 9aa4156

File tree

5 files changed

+105
-101
lines changed

5 files changed

+105
-101
lines changed

VERSION_HISTORY.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Version History
2+
3+
## `Version 0.0.1-alpha.`
4+
Pre-release.
5+
- Use net/http of api client.
6+
7+
Feature:
8+
- Verify AccesToken Token

model.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package lineloginadminsdk
22

3-
4-
type AccessTokeVerify struct {
5-
ClientID string `json:"client_id"`
6-
ExpiresIn int `json:"expires_in"`
7-
Scope string `json:"scope"`
8-
}
3+
type AccessTokenVerify struct {
4+
ClientID string `json:"client_id"`
5+
ExpiresIn int `json:"expires_in"`
6+
Scope string `json:"scope"`
7+
}
8+
type IDTokenVerify struct {
9+
ISS string `json:"iss"`
10+
Aud string `json:"aud"`
11+
Exp int `json:"exp"`
12+
Iat int `json:"iat"`
13+
Sub string `json:"sub"`
14+
Name string `json:"name"`
15+
Picture string `json:"picture"`
16+
Amr []string `json:"amr"`
17+
email string `json:"email"`
18+
}

token.go

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package lineloginadminsdk
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/StandUpCode/LineLoginAdminSDK/utils"
@@ -15,11 +16,28 @@ func VerifyAccessToken(accessToken string) error {
1516
if err != nil {
1617
panic(err)
1718
}
18-
1919
return nil
2020

2121
}
2222

23-
func VerifyIDToken(idToken string) error {
23+
func VerifyIDToken(IDToken , ChannelID string) (*IDTokenVerify, error){
24+
25+
uri := "https://api.line.me/oauth2/v2.1/verify"
26+
payload := fmt.Sprintf("id_token=%s&client_id=%s", IDToken, ChannelID)
27+
28+
content_type := "application/x-www-form-urlencoded"
29+
result, err := utils.Post(uri, payload, content_type)
2430

25-
}
31+
if err != nil {
32+
panic(err)
33+
}
34+
verify_response := IDTokenVerify{}
35+
err = json.Unmarshal(result, &verify_response)
36+
if err != nil {
37+
//fmt.Printf("Error: %s", err)
38+
return nil, err
39+
}
40+
41+
return &verify_response, nil
42+
}
43+

token_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ var AccessTokenTestData = []struct {
1010
{"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FjY2Vzcy5saW5lLm1lIiwic3ViIjoiVWM3YTc0MTUyYjI5MDAzMDkwNGJiNWM1ZGExM2NiNDRmIiwiYXVkIjoiMTY1NjcxMTg0NCIsImV4cCI6MTY0OTA1NTY3OSwiaWF0IjoxNjQ5MDUyMDc5LCJhbXIiOlsibGluZXNzbyJdLCJuYW1lIjoiQVJNIiwicGljdHVyZSI6Imh0dHBzOi8vcHJvZmlsZS5saW5lLXNjZG4ubmV0LzBoQldfb0tuODBIWGhpRmdsWmhCVmlMMTVURXhVVk9Cc3dHblFDR2tFVlJFMUpJQWtwV25SYVMwQVJFeDhjY1EwbURTTlJIQlVlUzBrZiJ9.KJpi1tu4nbOagjiDJhau5rnm4c_I55eB1PcNh9FALvY"},
1111
}
1212

13+
var IDTestData = []struct {
14+
IDToken string
15+
ChannelID string
16+
}{
17+
{
18+
"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL2FjY2Vzcy5saW5lLm1lIiwic3ViIjoiVWM3YTc0MTUyYjI5MDAzMDkwNGJiNWM1ZGExM2NiNDRmIiwiYXVkIjoiMTY1NjcxMTg0NCIsImV4cCI6MTY0OTA2MjIyNiwiaWF0IjoxNjQ5MDU4NjI2LCJhbXIiOlsibGluZXNzbyJdLCJuYW1lIjoiQVJNIiwicGljdHVyZSI6Imh0dHBzOi8vcHJvZmlsZS5saW5lLXNjZG4ubmV0LzBoQldfb0tuODBIWGhpRmdsWmhCVmlMMTVURXhVVk9Cc3dHblFDR2tFVlJFMUpJQWtwV25SYVMwQVJFeDhjY1EwbURTTlJIQlVlUzBrZiJ9.a36c5JW0fzDx5RgYECsxLKtBoqgc7q93nZ_RrHmzeqo",
19+
"1656711844",
20+
},
21+
}
22+
1323
func TestVerifyAccessToken(t *testing.T) {
1424
for _, data := range AccessTokenTestData {
1525
var err error
@@ -21,3 +31,15 @@ func TestVerifyAccessToken(t *testing.T) {
2131
}
2232

2333
}
34+
35+
func TestVerifyIDToken(t *testing.T) {
36+
for _, data := range IDTestData {
37+
var err error
38+
39+
if _,err = VerifyIDToken(data.IDToken, data.ChannelID); err != nil {
40+
t.Errorf("Unable to verify accesstoken: %v", err)
41+
}
42+
43+
}
44+
45+
}

utils/requests.go

Lines changed: 38 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -2,112 +2,58 @@ package utils
22

33
import (
44
"fmt"
5-
"io/ioutil"
6-
// "log"
7-
"net/http"
85

9-
6+
"github.com/valyala/fasthttp"
107
)
118

12-
var statusMessage = []string{
13-
100: "Continue",
14-
101: "Switching Protocols",
15-
102: "Processing",
16-
103: "Early Hints",
17-
200: "OK",
18-
201: "Created",
19-
202: "Accepted",
20-
203: "Non-Authoritative Information",
21-
204: "No Content",
22-
205: "Reset Content",
23-
206: "Partial Content",
24-
207: "Multi-Status",
25-
208: "Already Reported",
26-
226: "IM Used",
27-
300: "Multiple Choices",
28-
301: "Moved Permanently",
29-
302: "Found",
30-
303: "See Other",
31-
304: "Not Modified",
32-
305: "Use Proxy",
33-
306: "Switch Proxy",
34-
307: "Temporary Redirect",
35-
308: "Permanent Redirect",
36-
400: "Bad Request",
37-
401: "Unauthorized",
38-
402: "Payment Required",
39-
403: "Forbidden",
40-
404: "Not Found",
41-
405: "Method Not Allowed",
42-
406: "Not Acceptable",
43-
407: "Proxy Authentication Required",
44-
408: "Request Timeout",
45-
409: "Conflict",
46-
410: "Gone",
47-
411: "Length Required",
48-
412: "Precondition Failed",
49-
413: "Request Entity Too Large",
50-
414: "Request URI Too Long",
51-
415: "Unsupported Media Type",
52-
416: "Requested Range Not Satisfiable",
53-
417: "Expectation Failed",
54-
418: "I'm a teapot",
55-
421: "Misdirected Request",
56-
422: "Unprocessable Entity",
57-
423: "Locked",
58-
424: "Failed Dependency",
59-
426: "Upgrade Required",
60-
428: "Precondition Required",
61-
429: "Too Many Requests",
62-
431: "Request Header Fields Too Large",
63-
451: "Unavailable For Legal Reasons",
64-
500: "Internal Server Error",
65-
501: "Not Implemented",
66-
502: "Bad Gateway",
67-
503: "Service Unavailable",
68-
504: "Gateway Timeout",
69-
505: "HTTP Version Not Supported",
70-
506: "Variant Also Negotiates",
71-
507: "Insufficient Storage",
72-
508: "Loop Detected",
73-
510: "Not Extended",
74-
511: "Network Authentication Required",
75-
}
76-
779
func Get(uri string) error {
78-
client := http.Client{}
79-
req, err := http.NewRequest("GET", uri, nil)
10+
req := fasthttp.AcquireRequest()
11+
defer fasthttp.ReleaseRequest(req)
12+
req.SetRequestURI(uri)
13+
14+
// Acquire a response instance
15+
resp := fasthttp.AcquireResponse()
16+
defer fasthttp.ReleaseResponse(resp)
8017

81-
resp, err := client.Do(req)
82-
83-
body, err := ioutil.ReadAll(resp.Body)
18+
err := fasthttp.Do(req, resp)
8419
if err != nil {
85-
panic(err)
20+
fmt.Printf("Client get failed: %s\n", err)
21+
return err
8622
}
87-
fmt.Println(string(body))
23+
if resp.StatusCode() != fasthttp.StatusOK {
24+
fmt.Printf("Expected status code %d but got %d\n", fasthttp.StatusOK, resp.StatusCode())
25+
return err
26+
}
27+
body := resp.Body()
28+
29+
fmt.Printf("Response body is: %s", body)
8830

8931
return nil
32+
9033
}
9134

92-
// func Post(uri string, payload string, content_type string) []byte {
93-
// req := fasthttp.AcquireRequest()
94-
// defer fasthttp.ReleaseRequest(req)
35+
func Post(uri string, payload string, content_type string) ([]byte, error) {
36+
req := fasthttp.AcquireRequest()
37+
defer fasthttp.ReleaseRequest(req)
9538

96-
// resp := fasthttp.AcquireResponse()
97-
// defer fasthttp.ReleaseResponse(resp)
39+
resp := fasthttp.AcquireResponse()
40+
defer fasthttp.ReleaseResponse(resp)
9841

99-
// req.Header.SetMethod("POST")
100-
// req.Header.SetContentType(content_type)
42+
req.Header.SetMethod("POST")
43+
req.Header.SetContentType(content_type)
10144

102-
// req.SetRequestURI(uri)
103-
// req.SetBody([]byte(payload))
45+
req.SetRequestURI(uri)
10446

105-
// if err := fasthttp.Do(req, resp); err != nil {
106-
// log.Println("Error: ", err)
107-
// }
108-
// result := resp.Body()
109-
// // fmt.Println(result)
47+
req.SetBody([]byte(payload))
11048

111-
// return result
49+
err := fasthttp.Do(req, resp)
50+
if err != nil {
51+
fmt.Printf("Client get failed: %s\n", err)
52+
return nil, err
53+
}
54+
body := resp.Body()
55+
fmt.Printf("Response body is: %s", body)
11256

113-
//}
57+
return body, nil
58+
59+
}

0 commit comments

Comments
 (0)