Skip to content

Commit 88ab615

Browse files
committed
Initial import
0 parents  commit 88ab615

File tree

7 files changed

+223
-0
lines changed

7 files changed

+223
-0
lines changed

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.dll
4+
*.so
5+
*.dylib
6+
7+
# Test binary, build with `go test -c`
8+
*.test
9+
10+
# Output of the go coverage tool, specifically when used with LiteIDE
11+
*.out
12+
13+
vendor/
14+
dist/
15+
16+
# gh private key
17+
private_key.pem
18+
19+
# binary
20+
fidelius

.goreleaser.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
builds:
2+
- binary: fidelius
3+
goos:
4+
- windows
5+
- darwin
6+
- linux
7+
goarch:
8+
- amd64

Gopkg.lock

Lines changed: 15 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Gopkg.toml example
2+
#
3+
# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md
4+
# for detailed Gopkg.toml documentation.
5+
#
6+
# required = ["github.com/user/thing/cmd/thing"]
7+
# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"]
8+
#
9+
# [[constraint]]
10+
# name = "github.com/user/project"
11+
# version = "1.0.0"
12+
#
13+
# [[constraint]]
14+
# name = "github.com/user/project2"
15+
# branch = "dev"
16+
# source = "github.com/myfork/project2"
17+
#
18+
# [[override]]
19+
# name = "github.com/x/y"
20+
# version = "2.4.0"
21+
#
22+
# [prune]
23+
# non-go = false
24+
# go-tests = true
25+
# unused-packages = true
26+
27+
28+
[[constraint]]
29+
name = "github.com/dgrijalva/jwt-go"
30+
version = "3.1.0"
31+
32+
[prune]
33+
go-tests = true
34+
unused-packages = true

LICENSE

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <https://unlicense.org>

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Fidelius
2+
3+
## Usage
4+
5+
If you want to generate a temporary access token:
6+
7+
$ fidelius --gh-integration-id=1234 --gh-installation-id=5678 --gh-private-key='/root/fidelius-charm.2018-02-12.private-key.pem'
8+
# will print the token
9+
v1.437ad23ce1a4a71b1f3222bc198de653619a9570
10+
11+
If you want to generate a proper `.gitconfig` file:
12+
13+
$ fidelius --gh-integration-id=1234 --gh-installation-id=5678 --gh-private-key='/root/fidelius-charm.2018-02-12.private-key.pem' --git-config-out='/root/.gitconfig'
14+
15+
or if you want to modify an existing `.gitconfig`:
16+
17+
$ export GH_TOKEN=$(fidelius --gh-integration-id=1234 --gh-installation-id=5678 --gh-private-key='/root/fidelius-charm.2018-02-12.private-key.pem')
18+
$ git config --global url.https://x-access-token:[email protected]/.insteadOf https://github.com/
19+
20+
## Name
21+
22+
[Fidelius Charm](http://harrypotter.wikia.com/wiki/Fidelius_Charm)
23+
24+
## License
25+
26+
Released under Unlicense. Check the `LICENSE` for more details.

main.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"html/template"
8+
"io/ioutil"
9+
"log"
10+
"net/http"
11+
"os"
12+
"path/filepath"
13+
"time"
14+
15+
jwt "github.com/dgrijalva/jwt-go"
16+
)
17+
18+
var (
19+
OutDir string
20+
IntegrationId string
21+
InstallationId int64
22+
PrivateKeyFile string
23+
)
24+
25+
type GHResponse struct {
26+
Token string `json:"token,omitempty"`
27+
ExpiresAt time.Time `json:"expires_at,omitempty"`
28+
}
29+
30+
func init() {
31+
flag.StringVar(&OutDir, "git-config-out", "", "path where we will write the .gitconfig file")
32+
flag.StringVar(&IntegrationId, "gh-integration-id", "1234", "Github Integration's id")
33+
flag.StringVar(&PrivateKeyFile, "gh-private-key", "private_key.pem", "Full path to the Github Integration's private key")
34+
flag.Int64Var(&InstallationId, "gh-installation-id", 5678, "Github Integtation's installation id")
35+
}
36+
37+
func fatalErr(err error) {
38+
if err != nil {
39+
log.Fatalln(err)
40+
}
41+
}
42+
43+
func main() {
44+
flag.Parse()
45+
data, err := ioutil.ReadFile(PrivateKeyFile)
46+
fatalErr(err)
47+
key, err := jwt.ParseRSAPrivateKeyFromPEM(data)
48+
fatalErr(err)
49+
token, err := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.StandardClaims{
50+
IssuedAt: time.Now().Unix(),
51+
ExpiresAt: time.Now().Add(10 * time.Minute).Unix(),
52+
Issuer: IntegrationId,
53+
}).SignedString(key)
54+
fatalErr(err)
55+
56+
var result GHResponse
57+
req, err := http.NewRequest("POST", fmt.Sprintf("https://api.github.com/installations/%d/access_tokens", InstallationId), nil)
58+
fatalErr(err)
59+
60+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
61+
req.Header.Add("Accept", "application/vnd.github.machine-man-preview+json")
62+
fatalErr(JsonResponse(req, &result))
63+
64+
if OutDir == "" {
65+
fmt.Print(result.Token)
66+
} else {
67+
path, err := filepath.Abs(OutDir)
68+
fatalErr(err)
69+
f, err := os.Create(path)
70+
fatalErr(err)
71+
defer f.Close()
72+
t := template.Must(template.New("t1").
73+
Parse(`[url "https://x-access-token:{{.}}@github.com/"]
74+
insteadOf = https://github.com/`))
75+
fatalErr(t.Execute(f, result.Token))
76+
}
77+
}
78+
79+
func JsonResponse(req *http.Request, target interface{}) error {
80+
client := &http.Client{
81+
Timeout: 10 * time.Second,
82+
}
83+
resp, err := client.Do(req)
84+
if err != nil {
85+
return err
86+
}
87+
defer resp.Body.Close()
88+
if resp.StatusCode != http.StatusCreated {
89+
data, err := ioutil.ReadAll(resp.Body)
90+
if err != nil {
91+
fmt.Println(err)
92+
}
93+
log.Fatalln(string(data))
94+
}
95+
return json.NewDecoder(resp.Body).Decode(target)
96+
}

0 commit comments

Comments
 (0)