github.com/line/line-bot-sdk-go/v7@v7.21.0/linebot/oauth2.go (about) 1 // Copyright 2020 LINE Corporation 2 // 3 // LINE Corporation licenses this file to you under the Apache License, 4 // version 2.0 (the "License"); you may not use this file except in compliance 5 // with the License. You may obtain a copy of the License at: 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package linebot 16 17 import ( 18 "context" 19 "net/url" 20 "strings" 21 ) 22 23 const clientAssertionTypeJWT = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer" 24 25 // IssueAccessTokenV2 method 26 func (client *Client) IssueAccessTokenV2(clientAssertion string) *IssueAccessTokenV2Call { 27 return &IssueAccessTokenV2Call{ 28 c: client, 29 clientAssertion: clientAssertion, 30 } 31 } 32 33 // IssueAccessTokenV2Call type 34 type IssueAccessTokenV2Call struct { 35 c *Client 36 ctx context.Context 37 38 clientAssertion string 39 } 40 41 // WithContext method 42 func (call *IssueAccessTokenV2Call) WithContext(ctx context.Context) *IssueAccessTokenV2Call { 43 call.ctx = ctx 44 return call 45 } 46 47 // Do method 48 func (call *IssueAccessTokenV2Call) Do() (*AccessTokenResponse, error) { 49 vs := url.Values{} 50 vs.Set("grant_type", "client_credentials") 51 vs.Set("client_assertion_type", clientAssertionTypeJWT) 52 vs.Set("client_assertion", call.clientAssertion) 53 body := strings.NewReader(vs.Encode()) 54 55 res, err := call.c.postForm(call.ctx, APIEndpointIssueAccessTokenV2, body) 56 if err != nil { 57 return nil, err 58 } 59 defer closeResponse(res) 60 return decodeToAccessTokenResponse(res) 61 } 62 63 // GetAccessTokensV2 method 64 func (client *Client) GetAccessTokensV2(clientAssertion string) *GetAccessTokensV2Call { 65 return &GetAccessTokensV2Call{ 66 c: client, 67 clientAssertion: clientAssertion, 68 } 69 } 70 71 // GetAccessTokensV2Call type 72 type GetAccessTokensV2Call struct { 73 c *Client 74 ctx context.Context 75 76 clientAssertion string 77 } 78 79 // WithContext method 80 func (call *GetAccessTokensV2Call) WithContext(ctx context.Context) *GetAccessTokensV2Call { 81 call.ctx = ctx 82 return call 83 } 84 85 // Do method 86 func (call *GetAccessTokensV2Call) Do() (*AccessTokensResponse, error) { 87 vs := url.Values{} 88 vs.Set("client_assertion_type", clientAssertionTypeJWT) 89 vs.Set("client_assertion", call.clientAssertion) 90 91 res, err := call.c.get(call.ctx, call.c.endpointBase, APIEndpointGetAccessTokensV2, vs) 92 // body := strings.NewReader(vs.Encode()) 93 // res, err := call.c.postForm(call.ctx, APIEndpointGetAccessTokensV2, body) 94 if err != nil { 95 return nil, err 96 } 97 defer closeResponse(res) 98 return decodeToAccessTokensResponse(res) 99 } 100 101 // RevokeAccessTokenV2 method 102 func (client *Client) RevokeAccessTokenV2(channelID, channelSecret, accessToken string) *RevokeAccessTokenV2Call { 103 return &RevokeAccessTokenV2Call{ 104 c: client, 105 accessToken: accessToken, 106 channelID: channelID, 107 channelSecret: channelSecret, 108 } 109 } 110 111 // RevokeAccessTokenV2Call type 112 type RevokeAccessTokenV2Call struct { 113 c *Client 114 ctx context.Context 115 116 accessToken string 117 channelID string 118 channelSecret string 119 } 120 121 // WithContext method 122 func (call *RevokeAccessTokenV2Call) WithContext(ctx context.Context) *RevokeAccessTokenV2Call { 123 call.ctx = ctx 124 return call 125 } 126 127 // Do method 128 func (call *RevokeAccessTokenV2Call) Do() (*BasicResponse, error) { 129 vs := url.Values{} 130 vs.Set("access_token", call.accessToken) 131 vs.Set("client_id", call.channelID) 132 vs.Set("client_secret", call.channelSecret) 133 body := strings.NewReader(vs.Encode()) 134 135 res, err := call.c.postForm(call.ctx, APIEndpointRevokeAccessTokenV2, body) 136 if err != nil { 137 return nil, err 138 } 139 defer closeResponse(res) 140 return decodeToBasicResponse(res) 141 }