vendor/github.com/McKael/madon/v3/app.go
changeset 265 05c40b36d3b2
parent 242 2a9ec03fe5a1
child 268 4dd196a4ee7c
equal deleted inserted replaced
264:8f478162d991 265:05c40b36d3b2
       
     1 /*
       
     2 Copyright 2017-2018 Mikael Berthe
       
     3 Copyright 2017 Ollivier Robert
       
     4 
       
     5 Licensed under the MIT license.  Please see the LICENSE file is this directory.
       
     6 */
       
     7 
       
     8 package madon
       
     9 
       
    10 import (
       
    11 	"net/url"
       
    12 	"strings"
       
    13 
       
    14 	"github.com/pkg/errors"
       
    15 	"github.com/sendgrid/rest"
       
    16 )
       
    17 
       
    18 type registerApp struct {
       
    19 	ID           int64  `json:"id,string"`
       
    20 	ClientID     string `json:"client_id"`
       
    21 	ClientSecret string `json:"client_secret"`
       
    22 }
       
    23 
       
    24 // buildInstanceURL creates the URL from the instance name or cleans up the
       
    25 // provided URL
       
    26 func buildInstanceURL(instanceName string) (string, error) {
       
    27 	if instanceName == "" {
       
    28 		return "", errors.New("no instance provided")
       
    29 	}
       
    30 
       
    31 	instanceURL := instanceName
       
    32 	if !strings.Contains(instanceURL, "/") {
       
    33 		instanceURL = "https://" + instanceName
       
    34 	}
       
    35 
       
    36 	u, err := url.ParseRequestURI(instanceURL)
       
    37 	if err != nil {
       
    38 		return "", err
       
    39 	}
       
    40 
       
    41 	u.Path = ""
       
    42 	u.RawPath = ""
       
    43 	u.RawQuery = ""
       
    44 	u.Fragment = ""
       
    45 	return u.String(), nil
       
    46 }
       
    47 
       
    48 // NewApp registers a new application with a given instance
       
    49 func NewApp(name, website string, scopes []string, redirectURI, instanceName string) (mc *Client, err error) {
       
    50 	instanceURL, err := buildInstanceURL(instanceName)
       
    51 	if err != nil {
       
    52 		return nil, err
       
    53 	}
       
    54 
       
    55 	mc = &Client{
       
    56 		Name:        name,
       
    57 		InstanceURL: instanceURL,
       
    58 		APIBase:     instanceURL + currentAPIPath,
       
    59 	}
       
    60 
       
    61 	params := make(apiCallParams)
       
    62 	params["client_name"] = name
       
    63 	if website != "" {
       
    64 		params["website"] = website
       
    65 	}
       
    66 	params["scopes"] = strings.Join(scopes, " ")
       
    67 	if redirectURI != "" {
       
    68 		params["redirect_uris"] = redirectURI
       
    69 	} else {
       
    70 		params["redirect_uris"] = NoRedirect
       
    71 	}
       
    72 
       
    73 	var app registerApp
       
    74 	if err := mc.apiCall("v1/apps", rest.Post, params, nil, nil, &app); err != nil {
       
    75 		return nil, err
       
    76 	}
       
    77 
       
    78 	mc.ID = app.ClientID
       
    79 	mc.Secret = app.ClientSecret
       
    80 
       
    81 	return
       
    82 }
       
    83 
       
    84 // RestoreApp recreates an application client with existing secrets
       
    85 func RestoreApp(name, instanceName, appID, appSecret string, userToken *UserToken) (mc *Client, err error) {
       
    86 	instanceURL, err := buildInstanceURL(instanceName)
       
    87 	if err != nil {
       
    88 		return nil, err
       
    89 	}
       
    90 
       
    91 	return &Client{
       
    92 		Name:        name,
       
    93 		InstanceURL: instanceURL,
       
    94 		APIBase:     instanceURL + currentAPIPath,
       
    95 		ID:          appID,
       
    96 		Secret:      appSecret,
       
    97 		UserToken:   userToken,
       
    98 	}, nil
       
    99 }