-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bc10c0
commit 679079b
Showing
3 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
name: Renew facebook token | ||
|
||
on: | ||
schedule: | ||
- cron: "0 0 1 * *" | ||
|
||
permissions: | ||
actions: read | ||
|
||
jobs: | ||
renew: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: ruby/[email protected] | ||
with: | ||
ruby-version: '2.7' | ||
bundler-cache: true | ||
- name: Update the facebook token | ||
env: | ||
FACEBOOK_APP_ID: ${{ secrets.FACEBOOK_APP_ID }} | ||
FACEBOOK_CLIENT_SECRET: ${{ secrets.FACEBOOK_CLIENT_SECRET }} | ||
FACEBOOK_TOKEN: ${{ secrets.FACEBOOK_TOKEN }} | ||
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} | ||
run: bundle exec ruby renew.rb |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'json' | ||
require 'octokit' | ||
require 'rbnacl' | ||
require 'base64' | ||
|
||
require './env_utils' | ||
|
||
access_token = get_env_or_exit('FACEBOOK_TOKEN') | ||
app_id = get_env_or_exit('FACEBOOK_APP_ID') | ||
app_secret = get_env_or_exit('FACEBOOK_CLIENT_SECRET') | ||
|
||
uri = URI('https://graph.facebook.com/oauth/access_token') | ||
uri.query = URI.encode_www_form({ | ||
grant_type: 'fb_exchange_token', | ||
client_id: app_id, | ||
client_secret: app_secret, | ||
fb_exchange_token: access_token | ||
}) | ||
|
||
res = Net::HTTP.get_response(uri) | ||
if res.is_a?(Net::HTTPSuccess) | ||
new_token = JSON.parse(res.body)['access_token'] | ||
else | ||
puts "Bad response: #{res}" | ||
exit(-1) | ||
end | ||
|
||
github_token = get_env_or_exit('GITHUB_TOKEN') | ||
|
||
# Provide authentication credentials | ||
client = Octokit::Client.new(access_token: github_token) | ||
|
||
def create_box(public_key) | ||
b64_key = RbNaCl::PublicKey.new(Base64.decode64(public_key[:key])) | ||
{ | ||
key_id: public_key[:key_id], | ||
box: RbNaCl::Boxes::Sealed.from_public_key(b64_key) | ||
} | ||
end | ||
|
||
repo = client.repo 'atelierdesmedias/atelierdesmedias.github.io' | ||
|
||
secret = { name: 'FACEBOOK_TOKEN', value: new_token } | ||
public_key = client.get_public_key(repo.id) | ||
puts 'public key:', public_key, public_key[:key], public_key[:key_id] | ||
box = create_box(public_key) | ||
puts 'box:', box[:key_id], box[:box] | ||
puts 'after' | ||
encrypted = box[:box].encrypt(secret[:value]) | ||
encrypted_value = Base64.strict_encode64(encrypted) | ||
puts 'encrypted_value:', encrypted_value | ||
puts client.create_or_update_secret( | ||
repo.id, secret[:name], | ||
key_id: box[:key_id], encrypted_value: encrypted_value | ||
) | ||
puts client.last_response.status |