Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatic renew facebook token #36

Merged
merged 2 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions .github/workflows/renew.yml
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ source 'https://rubygems.org'
gem 'jekyll', '~> 4.2'
gem 'koala', '~> 3.1.0'
gem 'mini_magick', '~> 4.11'
gem 'octokit', '~> 4.0'
gem 'octokit', '~> 5.0'
gem 'rake', '~> 13.0.1'
gem 'rbnacl', '~> 7.1.1'
gem 'rubocop', '~> 1.22.3'
Expand Down
12 changes: 6 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ GEM
racc (~> 1.4)
nokogumbo (2.0.5)
nokogiri (~> 1.8, >= 1.8.4)
octokit (4.22.0)
faraday (>= 0.9)
sawyer (~> 0.8.0, >= 0.5.3)
octokit (5.6.1)
faraday (>= 1, < 3)
sawyer (~> 0.9)
parallel (1.21.0)
parser (3.0.2.0)
ast (~> 2.4.1)
Expand Down Expand Up @@ -141,9 +141,9 @@ GEM
safe_yaml (1.0.5)
sassc (2.4.0)
ffi (~> 1.9)
sawyer (0.8.2)
sawyer (0.9.2)
addressable (>= 2.3.5)
faraday (> 0.8, < 2.0)
faraday (>= 0.17.3, < 3)
stringex (2.8.5)
terminal-table (2.0.0)
unicode-display_width (~> 1.1, >= 1.1.1)
Expand All @@ -166,7 +166,7 @@ DEPENDENCIES
jekyll-tidy (~> 0.2.2)
koala (~> 3.1.0)
mini_magick (~> 4.11)
octokit (~> 4.0)
octokit (~> 5.0)
rake (~> 13.0.1)
rbnacl (~> 7.1.1)
rubocop (~> 1.22.3)
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ bundle exec jekyll serve
- Setup page id in `.github/workflows/deploy.yml`
- Setup application: <https://stackoverflow.com/questions/17197970/facebook-permanent-page-access-token>

The facebook token has to be refresh according to this link: <https://developers.facebook.com/docs/facebook-login/access-tokens/refreshing/>

## VSCode configuration

These plugin will help writing CSS respecting the rules: <https://marketplace.visualstudio.com/items?itemName=stylelint.vscode-stylelint>
Expand Down
58 changes: 58 additions & 0 deletions renew.rb
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