diff --git a/lp/.gitignore b/lp/.gitignore index 7638d86..1a174f7 100644 --- a/lp/.gitignore +++ b/lp/.gitignore @@ -32,8 +32,11 @@ npm-debug.log # we ignore priv/static. You may want to comment # this depending on your deployment strategy. # /priv/static/ +/priv/secret/* +/priv/cert/ # find the way to get your prod secret to prod server # leaving you an example /config/prod.exs -/config/prod.secret.exs \ No newline at end of file +/config/prod.secret.exs +/config/github.exs \ No newline at end of file diff --git a/lp/config/config.exs b/lp/config/config.exs index 0fe31b6..cc11394 100644 --- a/lp/config/config.exs +++ b/lp/config/config.exs @@ -15,6 +15,7 @@ config :lp, # Configures the endpoint config :lp, LpWeb.Endpoint, url: [host: "localhost"], + force_ssl: [hsts: true], secret_key_base: "DgokGSB84E7YMG0wLwe/hR51+TpWnqbdQmZjvGh6SIEDExzvOQS1IYiJoxTcI/XN", render_errors: [view: LpWeb.ErrorView, accepts: ~w(html json), layout: false], pubsub_server: Lp.PubSub, @@ -33,9 +34,16 @@ config :phoenix, :template_engines, md: PhoenixMarkdown.Engine # haml: PhoenixHaml.Engine # https://emmet.io eex should be good enough - # see also: https://github.com/chrismccord/phoenix_haml # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{Mix.env()}.exs" + +import_config "github.exs" +#_________________ +#import Config +#config :lp, +# client_id: "your_fancy_github_id", +# client_secret: "that_is_a_secret" +# \ No newline at end of file diff --git a/lp/config/dev.exs b/lp/config/dev.exs index f5aa752..2690f87 100644 --- a/lp/config/dev.exs +++ b/lp/config/dev.exs @@ -16,9 +16,9 @@ config :lp, Lp.Repo, # watchers to your application. For example, we use it # with webpack to recompile .js and .css sources. config :lp, LpWeb.Endpoint, - http: [port: 4000], + # http: [port: 80], https: [ - port: 4001, + port: 443, cipher_suite: :strong, keyfile: "priv/cert/selfsigned_key.pem", certfile: "priv/cert/selfsigned.pem" diff --git a/lp/config/prod.exs b/lp/config/prod.exs index 787df37..5a69f20 100644 --- a/lp/config/prod.exs +++ b/lp/config/prod.exs @@ -11,7 +11,7 @@ use Mix.Config # before starting your production server. config :lp, LpWeb.Endpoint, # url: [host: "example.com", port: 80], - url: [host: "35.193.215.48", port: 9999], + url: [host: "35.193.215.48", port: 443], #usually you would not want to do that, but i'm lazy. adjust to your machine cache_static_manifest: "priv/static/cache_manifest.json" @@ -23,16 +23,16 @@ config :logger, level: :info # To get SSL working, you will need to add the `https` key # to the previous section and set your `:url` port to 443: # -# config :lp, LpWeb.Endpoint, -# ... -# url: [host: "example.com", port: 443], -# https: [ -# port: 443, -# cipher_suite: :strong, -# keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), -# certfile: System.get_env("SOME_APP_SSL_CERT_PATH"), -# transport_options: [socket_opts: [:inet6]] -# ] + # config :lp, LpWeb.Endpoint, + # ... + # url: [host: "example.com", port: 443], + # https: [ + # port: 443, + # cipher_suite: :strong, + # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"), + # certfile: System.get_env("SOME_APP_SSL_CERT_PATH"), + # transport_options: [socket_opts: [:inet6]] + # ] # # The `cipher_suite` is set to `:strong` to support only the # latest and more secure SSL ciphers. This means old browsers diff --git a/lp/lib/lp_web/controllers/git_controller.ex b/lp/lib/lp_web/controllers/git_controller.ex new file mode 100644 index 0000000..76dc9c1 --- /dev/null +++ b/lp/lib/lp_web/controllers/git_controller.ex @@ -0,0 +1,25 @@ +defmodule LpWeb.GitController do + @client_id Application.fetch_env!(:lp, :client_id) + @client_secret Application.fetch_env!(:lp, :client_secret) + use LpWeb, :controller + # @data "/priv/secret/github_credentials" |> File.stream! |> Enum.to_list + + def index(conn, _params) do + render(conn, "git.html") + end + def show(conn, _params) do + html(conn,"") + end + def oauth(conn, _params) do + # render(conn, "oauth2.html") + redirect(conn, "git.html") + end + def github(conn, _params) do + authorize_url = Github.Oauth2.Client.authorize_url!( + # config: [client_id: Application.fetch_env!(:lp, :client_id), client_secret: Application.fetch_env!(:lp, :client_secret)], + config: [client_id: @client_id, client_secret: @client_secret], + scope: "user:email" + ) + conn |> redirect(external: authorize_url) + end + end \ No newline at end of file diff --git a/lp/lib/lp_web/router.ex b/lp/lib/lp_web/router.ex index 72c67f5..175bd47 100644 --- a/lp/lib/lp_web/router.ex +++ b/lp/lib/lp_web/router.ex @@ -19,6 +19,9 @@ defmodule LpWeb.Router do get "/", OldController, :index # get "/table", TableController, :sample get "/table", TableController, :index + get "/git", GitController, :index + get "/github", GitController, :github + get "/creds_show", GitController, :show # get "old", OldController, :index # live "/", PageLive, :index diff --git a/lp/lib/lp_web/templates/git/git.html.eex b/lp/lib/lp_web/templates/git/git.html.eex new file mode 100644 index 0000000..42dbe86 --- /dev/null +++ b/lp/lib/lp_web/templates/git/git.html.eex @@ -0,0 +1,44 @@ + + + +

Github mock page

+ + +
iex> jwt_token = Github.Client.generate_jwt_token(app_id: 12345, private_key_filepath: "app.pem")
+
+iex> github_client = %Github.Client{jwt_token: "jwt_token"}
+
+iex> github_client |> Github.Apps.Installations.find!(67890)
+%Github.Client.Response{
+  status: 200,
+  headers: [...],
+  body: %{"id" => 12345, ...},
+  ...
+}
+ +
def github(conn, _params) do
+  authorize_url = Github.Oauth2.Client.authorize_url!(
+    config: [client_id: "client_id", client_secret: "client_secret"],
+    scope: "user:email"
+  )
+  conn |> redirect(external: authorize_url)
+end
+ +
def github_callback(conn, %{"code" => code}) do
+  access_token = Github.Oauth2.Client.access_token!(
+    config: [client_id: "client_id", client_secret: "client_secret"],
+    params: [code: code]
+  )
+
+  github_client = %Github.Client{access_token: access_token}
+
+  %{"login" => login} = github_client |> Github.Users.find!() |> Map.fetch!(:body)
+  [%{"email" => email} | _] = github_client |> Github.Users.Emails.list!() |> Map.fetch!(:body)
+  ...
+end
+

https://github.com/WorkflowCI/github#with-app-jwt-token (c)

+<%= link "GH", to: "/github" %> +<%= link "CredsGH", to: "/creds_show" %> +<%# <%= Github.(@url_get_param) %> + + \ No newline at end of file diff --git a/lp/lib/lp_web/templates/git/oauth2.html.eex b/lp/lib/lp_web/templates/git/oauth2.html.eex new file mode 100644 index 0000000..9ecd5d0 --- /dev/null +++ b/lp/lib/lp_web/templates/git/oauth2.html.eex @@ -0,0 +1,2 @@ +

Autorisation succeded

+todo. keep context \ No newline at end of file diff --git a/lp/lib/lp_web/templates/layout/root.html.eex b/lp/lib/lp_web/templates/layout/root.html.eex index 0cad13e..c1d7ede 100644 --- a/lp/lib/lp_web/templates/layout/root.html.eex +++ b/lp/lib/lp_web/templates/layout/root.html.eex @@ -23,6 +23,7 @@
  • GameCult
  • Table
  • Home
  • +
  • Git
  • diff --git a/lp/lib/lp_web/views/git_view.ex b/lp/lib/lp_web/views/git_view.ex new file mode 100644 index 0000000..4af481a --- /dev/null +++ b/lp/lib/lp_web/views/git_view.ex @@ -0,0 +1,3 @@ +defmodule LpWeb.GitView do + use LpWeb, :view + end \ No newline at end of file diff --git a/lp/mix.exs b/lp/mix.exs index 8e54275..3ded1de 100644 --- a/lp/mix.exs +++ b/lp/mix.exs @@ -48,8 +48,8 @@ defmodule Lp.MixProject do {:jason, "~> 1.0"}, {:plug_cowboy, "~> 2.0"}, {:phoenix_markdown, "~> 1.0"}, - # {:phoenix_haml, "~> 0.2"}, - + # {:ueberauth_github, "~> 0.7"}, + {:github, "~> 0.12.0-rc1"}, ] end diff --git a/lp/mix.lock b/lp/mix.lock index 7704173..35eb4f4 100644 --- a/lp/mix.lock +++ b/lp/mix.lock @@ -1,5 +1,6 @@ %{ "calliope": {:hex, :calliope, "0.4.2", "29d8585331f3c04f95d6e2f7c8367d660cfb6c2dc031bd3d22afb7e7201eb5c6", [:mix], [], "hexpm", "3e355b0057ce52e402172e3f37f739765450b9c7ec18bf84c56c12007363283c"}, + "certifi": {:hex, :certifi, "2.5.2", "b7cfeae9d2ed395695dd8201c57a2d019c0c43ecaf8b8bcb9320b40d6662f340", [:rebar3], [{:parse_trans, "~>3.3", [hex: :parse_trans, repo: "hexpm", optional: false]}], "hexpm", "3b3b5f36493004ac3455966991eaf6e768ce9884693d9968055aeeeb1e575040"}, "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm", "4a0850c9be22a43af9920a71ab17c051f5f7d45c209e40269a1938832510e4d9"}, "cowboy": {:hex, :cowboy, "2.8.0", "f3dc62e35797ecd9ac1b50db74611193c29815401e53bac9a5c0577bd7bc667d", [:rebar3], [{:cowlib, "~> 2.9.1", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "4643e4fba74ac96d4d152c75803de6fad0b3fa5df354c71afdd6cbeeb15fac8a"}, "cowlib": {:hex, :cowlib, "2.9.1", "61a6c7c50cf07fdd24b2f45b89500bb93b6686579b069a89f88cb211e1125c78", [:rebar3], [], "hexpm", "e4175dc240a70d996156160891e1c62238ede1729e45740bdd38064dad476170"}, @@ -12,9 +13,19 @@ "file_system": {:hex, :file_system, "0.2.9", "545b9c9d502e8bfa71a5315fac2a923bd060fd9acb797fe6595f54b0f975fd32", [:mix], [], "hexpm", "3cf87a377fe1d93043adeec4889feacf594957226b4f19d5897096d6f61345d8"}, "floki": {:hex, :floki, "0.28.0", "0d0795a17189510ee01323e6990f906309e9fc6e8570219135211f1264d78c7f", [:mix], [{:html_entities, "~> 0.5.0", [hex: :html_entities, repo: "hexpm", optional: false]}], "hexpm", "db1549560874ebba5a6367e46c3aec5fedd41f2757ad6efe567efb04b4d4ee55"}, "gettext": {:hex, :gettext, "0.18.2", "7df3ea191bb56c0309c00a783334b288d08a879f53a7014341284635850a6e55", [:mix], [], "hexpm", "f9f537b13d4fdd30f3039d33cb80144c3aa1f8d9698e47d7bcbcc8df93b1f5c5"}, + "github": {:hex, :github, "0.12.0-rc1", "3b7c7efafb7ee2d6f584a129c2a42c38b1f00d12e289ba179f01865c6115e003", [:make, :mix], [{:httpoison, "~> 0.11", [hex: :httpoison, repo: "hexpm", optional: false]}, {:jason, "~> 1.1", [hex: :jason, repo: "hexpm", optional: true]}, {:joken, "~> 2.0.0-rc3", [hex: :joken, repo: "hexpm", optional: false]}, {:oauth2, "~> 0.9", [hex: :oauth2, repo: "hexpm", optional: false]}], "hexpm", "f48f1f08bd9a80ad71891ea70c76e3a4c165095d48facaaaf3cde555865d0420"}, + "hackney": {:hex, :hackney, "1.16.0", "5096ac8e823e3a441477b2d187e30dd3fff1a82991a806b2003845ce72ce2d84", [:rebar3], [{:certifi, "2.5.2", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "6.0.1", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "1.0.1", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~>1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.3.0", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "1.1.6", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}], "hexpm", "3bf0bebbd5d3092a3543b783bf065165fa5d3ad4b899b836810e513064134e18"}, "html_entities": {:hex, :html_entities, "0.5.1", "1c9715058b42c35a2ab65edc5b36d0ea66dd083767bef6e3edb57870ef556549", [:mix], [], "hexpm", "30efab070904eb897ff05cd52fa61c1025d7f8ef3a9ca250bc4e6513d16c32de"}, + "httpoison": {:hex, :httpoison, "0.13.0", "bfaf44d9f133a6599886720f3937a7699466d23bb0cd7a88b6ba011f53c6f562", [:mix], [{:hackney, "~> 1.8", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "4846958172d6401c4f34ecc5c2c4607b5b0d90b8eec8f6df137ca4907942ed0f"}, + "idna": {:hex, :idna, "6.0.1", "1d038fb2e7668ce41fbf681d2c45902e52b3cb9e9c77b55334353b222c2ee50c", [:rebar3], [{:unicode_util_compat, "0.5.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "a02c8a1c4fd601215bb0b0324c8a6986749f807ce35f25449ec9e69758708122"}, "jason": {:hex, :jason, "1.2.2", "ba43e3f2709fd1aa1dce90aaabfd039d000469c05c56f0b8e31978e03fa39052", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "18a228f5f0058ee183f29f9eae0805c6e59d61c3b006760668d8d18ff0d12179"}, + "joken": {:hex, :joken, "2.0.1", "ec9ab31bf660f343380da033b3316855197c8d4c6ef597fa3fcb451b326beb14", [:mix], [{:jose, "~> 1.9", [hex: :jose, repo: "hexpm", optional: false]}], "hexpm", "059be59550f93486b1b1f6bb54b7fa573354ee8d6adbb7244cad74d62e3fdf6a"}, + "jose": {:hex, :jose, "1.10.1", "16d8e460dae7203c6d1efa3f277e25b5af8b659febfc2f2eb4bacf87f128b80a", [:mix, :rebar3], [], "hexpm", "3c7ddc8a9394b92891db7c2771da94bf819834a1a4c92e30857b7d582e2f8257"}, + "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, "mime": {:hex, :mime, "1.4.0", "5066f14944b470286146047d2f73518cf5cca82f8e4815cf35d196b58cf07c47", [:mix], [], "hexpm", "75fa42c4228ea9a23f70f123c74ba7cece6a03b1fd474fe13f6a7a85c6ea4ff6"}, + "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, + "oauth2": {:hex, :oauth2, "0.9.4", "632e8e8826a45e33ac2ea5ac66dcc019ba6bb5a0d2ba77e342d33e3b7b252c6e", [:mix], [{:hackney, "~> 1.7", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "407c6b9f60aa0d01b915e2347dc6be78adca706a37f0c530808942da3b62e7af"}, + "parse_trans": {:hex, :parse_trans, "3.3.0", "09765507a3c7590a784615cfd421d101aec25098d50b89d7aa1d66646bc571c1", [:rebar3], [], "hexpm", "17ef63abde837ad30680ea7f857dd9e7ced9476cdd7b0394432af4bfc241b960"}, "phoenix": {:hex, :phoenix, "1.5.5", "9a5a197edc1828c5f138a8ef10524dfecc43e36ab435c14578b1e9b4bd98858c", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_html, "~> 2.13", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 2.0", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.10", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.2", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:plug_crypto, "~> 1.1.2 or ~> 1.2", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "b10eaf86ad026eafad2ee3dd336f0fb1c95a3711789855d913244e270bde463b"}, "phoenix_ecto": {:hex, :phoenix_ecto, "4.2.1", "13f124cf0a3ce0f1948cf24654c7b9f2347169ff75c1123f44674afee6af3b03", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.14.2 or ~> 2.15", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm", "478a1bae899cac0a6e02be1deec7e2944b7754c04e7d4107fc5a517f877743c0"}, "phoenix_haml": {:hex, :phoenix_haml, "0.2.3", "432b689ebb03dc821f00ae0ae5fb22e814aa8704e599763cbde538daf9f389f6", [:mix], [{:calliope, "~> 0.4.1", [hex: :calliope, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.1", [hex: :phoenix, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.3", [hex: :phoenix_html, repo: "hexpm", optional: false]}], "hexpm", "bc52d3a5800034aad469c18af317cc983b362760348ce2f50c395efc733a00ec"}, @@ -29,7 +40,9 @@ "plug_crypto": {:hex, :plug_crypto, "1.1.2", "bdd187572cc26dbd95b87136290425f2b580a116d3fb1f564216918c9730d227", [:mix], [], "hexpm", "6b8b608f895b6ffcfad49c37c7883e8df98ae19c6a28113b02aa1e9c5b22d6b5"}, "postgrex": {:hex, :postgrex, "0.15.6", "a464c72010a56e3214fe2b99c1a76faab4c2bb0255cabdef30dea763a3569aa2", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm", "f99268325ac8f66ffd6c4964faab9e70fbf721234ab2ad238c00f9530b8cdd55"}, "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm", "451d8527787df716d99dc36162fca05934915db0b6141bbdac2ea8d3c7afc7d7"}, + "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"}, "telemetry": {:hex, :telemetry, "0.4.2", "2808c992455e08d6177322f14d3bdb6b625fbcfd233a73505870d8738a2f4599", [:rebar3], [], "hexpm", "2d1419bd9dda6a206d7b5852179511722e2b18812310d304620c7bd92a13fcef"}, "telemetry_metrics": {:hex, :telemetry_metrics, "0.5.0", "1b796e74add83abf844e808564275dfb342bcc930b04c7577ab780e262b0d998", [:mix], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "31225e6ce7a37a421a0a96ec55244386aec1c190b22578bd245188a4a33298fd"}, "telemetry_poller": {:hex, :telemetry_poller, "0.5.1", "21071cc2e536810bac5628b935521ff3e28f0303e770951158c73eaaa01e962a", [:rebar3], [{:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "4cab72069210bc6e7a080cec9afffad1b33370149ed5d379b81c7c5f0c663fd4"}, + "unicode_util_compat": {:hex, :unicode_util_compat, "0.5.0", "8516502659002cec19e244ebd90d312183064be95025a319a6c7e89f4bccd65b", [:rebar3], [], "hexpm", "d48d002e15f5cc105a696cf2f1bbb3fc72b4b770a184d8420c8db20da2674b38"}, }