You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
75 lines
1.9 KiB
75 lines
1.9 KiB
|
2 weeks ago
|
/*
|
||
|
|
* Copyright 2024. Huawei Technologies Co., Ltd. All rights reserved.
|
||
|
|
*
|
||
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||
|
|
* you may not use this file except in compliance with the License.
|
||
|
|
* You may obtain a copy of the License at
|
||
|
|
*
|
||
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||
|
|
*
|
||
|
|
* Unless required by applicable law or agreed to in writing, software
|
||
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||
|
|
* See the License for the specific language governing permissions and
|
||
|
|
* limitations under the License.
|
||
|
|
*
|
||
|
|
*/
|
||
|
|
|
||
|
|
package harmonyos
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"io/ioutil"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type IapServer struct {
|
||
|
|
}
|
||
|
|
|
||
|
|
const (
|
||
|
|
// URL_ROOT IAP Server Root Url
|
||
|
|
URL_ROOT = "https://iap.cloud.huawei.com"
|
||
|
|
|
||
|
|
TIME_OUT = time.Second * 5 // TODO: Need to replace it with the actual business logic.
|
||
|
|
|
||
|
|
WHITE_SPACE = " "
|
||
|
|
)
|
||
|
|
|
||
|
|
func (iap *IapServer) httpPost(url string, bodyMap map[string]interface{}) (string, error) {
|
||
|
|
bodyData, err := json.Marshal(bodyMap)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
|
||
|
|
req, err := http.NewRequest("POST", url, bytes.NewReader(bodyData))
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
|
||
|
|
jwtGenerator := &JWTGenerator{}
|
||
|
|
jwt, err := jwtGenerator.GenJWT(bodyMap)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
req, err = iap.BuildAuthorization(req, jwt)
|
||
|
|
|
||
|
|
var RequestHttpClient = http.Client{Timeout: TIME_OUT}
|
||
|
|
response, err := RequestHttpClient.Do(req)
|
||
|
|
if err != nil {
|
||
|
|
return "", err
|
||
|
|
}
|
||
|
|
|
||
|
|
bodyBytes, err := ioutil.ReadAll(response.Body)
|
||
|
|
return string(bodyBytes), err
|
||
|
|
}
|
||
|
|
|
||
|
|
func (iap *IapServer) BuildAuthorization(req *http.Request, jwt string) (*http.Request, error) {
|
||
|
|
var authHeader = fmt.Sprintf("Bearer%s%s", WHITE_SPACE, jwt)
|
||
|
|
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
|
||
|
|
req.Header.Set("Authorization", authHeader)
|
||
|
|
return req, nil
|
||
|
|
}
|